This blog begins with the life cycle of the activity and the task (the concept of the activity stack) to illustrate the sequence in which the program executes (that is, the order of the Call life cycle method) in several cases (such as when a program starts or exits normally). Finally, the emphasis is on distinguishing several easily confusing life cycle methods.
First, the activity's life cycle in-depth understanding
At first we will see the following diagram:
The activity has 4 states throughout its lifecycle, and four states in three nested loops convert four states by invoking seven main methods:
1) Four different states
1. Activity (active/running) status
When the activity is running on the screen foreground (at the top of the current task's activity stack), it gets the focus to respond to the user's action, which is running, and only one moment of activity is active (active) or running
(Running) shape
2. Pause (Paused) status
It is paused when the activity loses focus but remains visible to the user (such as when there is another transparent activity or toast, alertdialog, etc.) on top of it. Paused activity is still alive (it retains all state and member information and remains connected to the window manager), but can be killed by the system when the system memory is very small
3. Stop (Stopped) status
Is in a stopped state when it is completely obscured by another activity, and it retains all the state and member information. It is not visible to the user, but it is often killed by the system when memory is needed elsewhere
4. Inactivity (Dead) status
If the activity has not been started, has been manually terminated, or has been inactive while the system is being reclaimed, you can invoke the "Finish" method in the program to terminate the activity manually. If it is being reclaimed by the system (according to the Recycle rule based on low memory), it may be due to low memory
2) 3 Nested loops
1. Activity complete life cycle: from the first call to OnCreate () until the call OnDestroy () ends
2, the activity's visual life cycle: from calling OnStart () to the corresponding call OnStop ()
Between the two methods, you can maintain the resources needed to display the activity. If you register a broadcast receiver in OnStart () to listen for changes that affect your UI, log off in OnStop ().
3, the activity of the foreground life cycle: from the call Onresume () to the corresponding call OnPause ().
second, the task of understanding:
Task is an activity stack, the bottom is the activity to start the entire task, the top of the stack is the current user can interact with the activity, when one activity launches another, the new activity is pushed into the stack, and becomes the activity currently running. The previous activity remains in the stack. When the user presses the back key, the current activity is out of the stack, and the previous one reverts to the currently running activity. The stack is actually the object, the activity in the stack will never reflow, will only press in or eject.
also note that all activity in the task is moved as a whole. The entire task (that is, the activity stack) can be moved to the foreground or back to the background. For example, for example, the current task has four activity── in the stack, three under the current activity. When the user presses the home key, it goes back to the application loader and selects a new application (that is, a new task). The current task is in the background and the root activity of the new task is displayed. Then, after a little while, the user returns to the application loader again and selects the previous application (the previous task). So the task, with all the four activity in the stack, once again to the front desk. however , when the user presses the back key, the screen does not show the activity that the user has just left (the root activity of the previous task). Instead, the topmost activity in the current task's stack pops up, and the last activity in the same task is displayed. Above, we have a deep experience in using Android machine.
The Android system is a multitasking (multi-task) operating system that allows you to listen to music on your phone while performing several other programs.
three, according to 3 nested loops we draw the procedure general flow:
program starts normally: OnCreate ()->onstart ()->onresume ();
Normal exit: OnPause ()->onstop ()->ondestory ()
One activity starts another activity:onpause ()->onstop (), and then returns: Onrestart ()->onstart ()->onresume ()
Program Press Back to exit: OnPause ()->onstop ()->ondestory (), then enter: OnCreate ()->onstart ()->onresume ();
Program Press Home exit: OnPause ()->onstop (), re-enter: Onrestart ()->onstart ()->onresume ();
So let's go through an example to understand:
There are 3 acitivity, respectively, with one,two (Transparent), three that one is the main activity when the application starts
When you start the first interface activity one, its order is
OnCreate (one)-OnStart (one)-Onresume (one)
When you click the "Open Transparent Activity" button, the order in which you go is
OnPause (one)-onCreate (double)-onStart (double)-onresume (both)
Back to the first interface, two will be killed, then the sequence of walking is
OnPause (Double)-Onactivityresult (one)-Onresume (one)-onStop (double)-OnDestroy (both)
Click on the "open Full screen Activity" button, then the sequence of walking is
OnPause (one)-onCreate (three)-OnStart (three)-Onresume (three)-OnStop (one)
Back to the first interface, three will be killed.
OnPause (three)-Onactivityresult (one)-Onrestart (one)-OnStart (one)-Onresume (one)-onStop (three)-OnDestroy (three)
Click Back to exit the app, it's in the order
OnPause (one)-onStop (one)-OnDestroy (one)
Four, several easy to confuse life cycle method:
Different data operations should be placed in different life cycle methods, the following are the characteristics of different methods.
when activity enters another state from one state, the system automatically calls the corresponding party below to inform users of this change
1, when the first time the activity is instantiated, the system will call,
OnCreate (Bundle savedinstancestate);
This method is called only 1 times throughout the life cycle
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
2. When the activity is visible and the user focus does not interact, the system calls
OnStart ();
3. When activity is stopped and then restarted, the system calls
Onrestart ();
4. When the activity is visible and the user's focus can interact, the system calls
Onresume ();
5, when the system launches another new activity, before the new activity is initiated by the system call to save the existing activity of persistent data, stop animation, etc., this implementation must be very fast. 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. This call
Onsaveinstancestate ()
Onsaveinstancestate () is used to save the state before the activity is killed, triggered before OnPause (), and when the system destroys the activity (which the user does not want to destroy) in order to save memory, the method needs to be rewritten when the
When the activity is instantiated again, the saved temporary state data is passed through OnCreate (Bundle savedinstancestate) because the Onsaveinstancestate () method is not always called and the trigger condition is (press
Home key, press the power button to turn off the screen, or in the case of a toggle screen), you should only rewrite Onsaveinstancestate () to record the temporary state of the activity, not the persistent data.
6. storing persistent data calls
OnPause ();
7. Called by the system when the activity is completely overwritten by the new activity
OnStop ();
8, when the activity (user call Finish () or system due to insufficient memory) by the system destroyed the system, (the entire life cycle is called only 1 times) to release the OnCreate () method created in the resource, such as the end of the thread and other calls
OnDestroy ();
"Android Personal Understanding (ix)"