Android four components (1)--activity

Source: Internet
Author: User

Four components classification

Activity (described in this article)

Service//Services

Content Provider//Contents Provider

Broadcastreceiver//Broadcast Receivers

Summarize----------------------------------------------------------------------

In an Android application, each activity typically corresponds to a visual interface that can display various UI controls for Android, which users can use to interact in order to accomplish a task. All operations in the activity are closely related to the user and are a component used to interact with the user. The specified control can be displayed by Setcontentview (View).

Description----------------------------------------------------------------------

Life cycle

Detailed Process description

(1) Start activity: The system calls the OnCreate method first, then calls the OnStart method, and finally calls Onresume,activity into the running state.

(2) The activity is currently covered by other activity or locked: The system calls the OnPause method, pausing the execution of the current activity.

(3) The current activity from the covered state back to the foreground or unlock screen: The system will call the Onresume method, again into the running state.

(4) The current activity to go to the new activity interface or press the home button back to the main screen, self-retired: The system will first call the OnPause method, and then call the OnStop method, into a stagnant state.

(5) The user backs back to this activity: the system calls the Onrestart method first, then calls the OnStart method, and finally calls the Onresume method, again into the running state.

(6) The current activity is in a covered state or the background is not visible, that is, the 2nd and 4th steps, the system memory is not enough, kill the current activity, and then the user returned to the current activity: Call OnCreate method Again, OnStart method, Onresume method, enter the running state.

(7) The user exits the current activity: The system calls the OnPause method first, then calls the OnStop method, and finally calls the Ondestory method to end the current activity.

--------------------------------------------------------------------------------

Activity 4 states, 7 important methods, and 3 nested loops throughout the life cycle

4 different states

(1) Active/running status when activity is in the current screen;

(2) Paused state, when the activity loses focus but is still visible to the user, that is, a non-full-screen or transparent activity on the activity's screen, and becomes the current focus. While the activity of paused is still in alive state, it retains all state and member information and connects to the window manager, but it can still kill the activity when the system is in very low memory. (3) stopped state, when activity is completely overwritten by another activity, it retains all status and member information. But it is no longer visible to the user, so its window will be hidden and when memory is needed elsewhere, the system will often kill the activity.

(4) When activity is a paused or stopped state, the system can drive it out of memory by requiring it to either end (call its finish () method) or kill it directly. When it is visible to the user again, it can only be fully restarted and restored to its previous state.

7 Important methods

(1) When the activity is instantiated for the first time, the system is called, and the entire life cycle is called only 1 times.

Typically used to initialize settings: 1, set the layout file to use for the activity 2, the button binding listener and other static settings operations

onCreate (Bundle savedinstancestate);

(2) When the activity is not visible, the user focus does not interact and the system calls

OnStart ();

(3) When activity is stopped and then restarted, the system calls

Onrestart ();

(4) When activity is visible and the user's focus can interact, the system calls

Onresume ();

(5) When the system initiates another new activity, it must be implemented very quickly by the system call to save the persisted data in the existing activity, stop the animation, etc. before the new activity starts. After the activity has been closed by the system instead of the user's own recovery memory. The user expects that when he returns to the activity again, it remains the way it was when he left. Using Onsaveinstancestate (), the Method Onsaveinstancestate () is used to save the state before the activity is killed, triggered before OnPause (), when the system destroys the activity in order to save memory ( The user does not want to destroy this method, when the activity is instantiated again by OnCreate (Bundle savedinstancestate) will have saved temporary state data passed in because Onsaveinstancestate ( The method is not always called, the trigger condition is (press the Home key, press the power button to turn off the screen, and in both cases), you should only rewrite Onsaveinstancestate () to record the temporary state of activity, rather than persistent data. You should use OnPause () to store persistent data.

onPause ();

(6) Called by the system when the activity is completely overwritten by the new activity

onStop ();

(7) When the activity (the user calls finish () or the system is destroyed by the system due to low memory) system calls, (the entire life cycle only calls 1 times) to release the resources created in the OnCreate () method, such as the end thread, etc.

OnDestroy ();

3 Nested Loops

(1) An activity full life cycle begins with the first call to OnCreate (Bundle) until OnDestroy () is called. Activity sets all the "global" states in OnCreate () to complete initialization, and frees all system resources in OnDestroy (). For example, if an activity has a thread running in the background to download data from the network, it creates that thread with onCreate () and destroys that thread with OnDestroy ().
(2) The visual life cycle of an activity is called from the OnStart () call until the corresponding onStop () call. During this time, the user can see the activity on the screen, although it may not be in the foreground or interacting with the user. In both of these methods, you can control the resources used to show the activity to the user. For example, you can register a broadcastreceiver in OnStart () to monitor changes that affect your UI, and unregister in OnStop (), when users can't see what your program is showing. The OnStart () and OnStop () methods can be called multiple times as the application is visible to the user.
(3) The foreground life cycle of an activity is called from Onresume () to the corresponding OnPause () call. During this time, the activity is at the top of the foreground and interacting with the user. Activity often transitions between pauses and recoveries-for example, when a device goes into hibernation or a new activity starts, it calls the OnPause () method. The Onresume () method is called when the activity obtains the result or receives a new intent. Therefore, the code in both of these methods should be lightweight.

Android four components (1)--activity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.