Transferred from: http://www.cnblogs.com/shaweng/archive/2012/07/03/2575302.html
Three loops
Provides two life cycle model diagrams for activity to help understand:
Figure 1
Figure 2
As shown in Figure 2, the activity life cycle is not difficult to see, in this diagram contains two layers of loops,
The first loop is OnPause, Onresume, OnPause,
The second loop is OnPause, Onresume, OnStop, OnStart, Onrestart, OnStop, and so on. We can consider these two layers of loops as the sub-lifecycles of the integration activity lifecycle. The first loop is called the focus life cycle, and the second layer is called the visual life cycle. In other words, the first loop loops through the acquisition and loss of activity focus, during which the activity is always visible. The second layer loops in the process of visible and invisible activity, which is accompanied by the acquisition and loss of the focus of activity. In other words, the activity is first displayed, then the focus is lost, then the focus is removed, and finally the activity becomes invisible due to the other activity being ejected. Therefore, activity has the following 3 life cycles:
- Overall life cycle: onCreate---OnDestroy.
- Visual life cycle: OnStop---OnPause.
- Focus life cycle: OnPause-Onresume.
Four Stages
The above 7 life cycle methods are called in a certain order in 4 stages, and these 4 phases are as follows:
- Start activity: Perform 3 life cycle methods in sequence at this stage: OnCreate, OnStart, and Onresume.
- Activity loses focus: If you enter another activity or application while the activity is focused, the current activity loses focus. At this stage, the OnPause and OnStop methods are executed in turn.
- Activity regain focus: If activity regain focus, 3 life cycle methods are executed sequentially: Onrestart, OnStart, and Onresume.
- Close activity: When activity is closed, the system executes 3 life cycle methods: OnPause, OnStop, and OnDestroy.
If there is no state change during these 4 phases of the life cycle approach, then the system will execute the life cycle methods in the 4 stages as described above, but if the state is changed during execution, the system will invoke the life cycle method in a more complex way.
The life cycle methods that can change the execution trajectory of the system during execution are OnPause and OnStop. If the activity gets the focus again during the execution of the OnPause method, it loses focus. Instead of executing the OnStop method, the system executes the corresponding life cycle method in the following order:
OnPause, onresume-> OnPause
If the activity gets the focus again during the execution of the OnStop method, it loses focus. Instead of executing the OnDestroy method, the system executes the corresponding life cycle method in the following order:
OnPause, Onresume, OnStart, Onrestart, OnStop, OnStop
As you can see in the activity life cycle shown in Figure 2, the system invokes the OnPause, OnStop, and Ondesktroy methods when terminating the application process. The OnPause method is at the top of the line, meaning that the activity may be terminated when the focus is lost, while the OnStop and OnDestroy methods may not have a chance to execute. Therefore, the current activity state should be saved in the OnPause method to ensure that the code that holds the activity state can be executed at any time when the process is terminated.
Seven Methods
The following is a detailed description of it (in the text in bold words will be frequently used in the text after the first occurrence of the concept will be explained, and then no longer described in detail):
1. void OnCreate (Bundle savedinstancestate)
Executed when the activity is first loaded. When we start a new program, the OnCreate event of its main form is executed. If the activity is destroyed (after OnDestroy) and then reloaded into the task, its OnCreate event is also re-executed. Note here that the parameter savedinstancestate (bundle type is a set of key-value pairs that you can see as a. NET (dictionary) is a useful design, due to the special features of mobile phone applications mentioned earlier, an activity is likely to be forced to switch to the background (swapping to the background means that the form is no longer visible to the user, but in fact it still exists in a task, A new activity, for example, presses the current task to "cloak" the current activity, or the user presses the home button to go back to the desktop, or other important events occur that cause the new activity to appear on top of the current activity, such as an incoming call interface. And if the user has not re-viewed the form for a period of time (Android can select the last 6 running programs by long pressing the home key, or the user clicks the program's run icon directly again, if the form's task and process is not destroyed by the system, it does not have to be reloaded. The activity at the top of the task is displayed directly, which is called re-viewing the form of a program, and the form, along with its task and process, may have been automatically destroyed by the system, and if you look at the form again, you re-execute the OnCreate event initialization form. And this time we might want the user to continue working on the state of the last time the form was opened, rather than starting from scratch. For example, the user in the text message when the sudden call, after the phone after the user to do some other things, such as saving the caller number to contact, and not immediately back to the text message editing interface, resulting in the text message editing interface is destroyed, when the user re-enter the text message program he may want to continue the last edit.in this case we can overwrite the activity's void Onsaveinstancestate (Bundle outstate) event by writing some state or information to outstate that we need to save before the form is destroyed. This way, when the form is re-executing OnCreate, the previously saved information is passed through savedinstancestate, and we can selectively use the information to initialize the form instead of starting from scratch.
2. void OnStart () activity becomes called when the user is visible on the screen.
Executes after the OnCreate event. Or, after the current form has been swapped to the background, the form has performed a OnStop event before the user re-views the form, but the form and its process are not destroyed, the user will perform the Onrestart event when the form is re-viewed, and the OnCreate event will be skipped. Executes the OnStart event of the form directly.
3. void Onresume () activity is invoked when the activity starts interacting with the user (whether it is a startup or a restart, the method is always called).
Executes after the OnStart event. Or, after the current form is swapped to the background, the form has not been destroyed and the OnStop event has not been executed (the form also continues to exist in the task) when the user re-views the form, and the OnCreate and OnStart events of the form are skipped and the Onresume event is executed directly.
4. void OnPause () activity is called when the CPU and other resources are paused or retracted, the method is used to save the active state, but also to protect the site, press stack bar!
The form is executed when it is swapped to the background.
5. void OnStop () is called when activity is stopped and turned into an invisible phase and subsequent life-cycle events.
Executes after the OnPause event. If the form is not re-viewed by the user for a period of time, the OnStop event of the form is executed, or the user presses the back key directly, removes the form from the current task, and executes the form's OnStop event.
6. void Onrestart () Called when activity is restarted. The activity is still in the stack, not the start of a new activity.
After the OnStop event executes, if the form and its process are not destroyed by the system, the user then re-views the form, the form's Onrestart event is executed, and the OnCreate event of the form is skipped after the Onrestart event to execute the OnStart event directly.
7. The void OnDestroy () activity is called when it is completely removed from system memory, and the method is called probably because someone calls the OnFinish () method directly or the system decides to stop the activity to free up resources!
Executed when the activity is destroyed. After the form's OnStop event, the activity is destroyed if the form is not viewed again.
Finally, a practical example is used to illustrate the various life cycles of activity. Suppose a program consists of 2 activity A and B, a is the start-up interface for this program. When the user starts the program, the process and the default task are created respectively, then a is pressed into the current task, followed by OnCreate, OnStart, Onresume event is presented to the user, the user chooses a feature in a to open Interface B, Interface B is pressed into the current task to cover up the A,a OnPause event execution, B's onCreate, OnStart, Onresume event execution, rendering interface B to the user, after user interface B is finished, use the back key to return to interface A, interface B is no longer visible, Interface B of the OnPause, OnStop, OnDestroy execution, a of the Onresume event is executed, rendering interface A to the user. At this time suddenly call, interface a OnPause event is executed, the telephone answering interface is presented to the user, the user after the call, and then press the home button back to the desktop, another program "Contacts", add contact information and do some other operations, the interface A is no longer visible, its The OnStop event was executed but was not destroyed. The user then clicked on our program from the menu, because a and its processes and tasks were not destroyed, A's Onrestart and OnStart events were executed, and A's Onresume event was executed, and a was presented to the user. After the user has finished using this, press the back key to return to the desktop, a onPause, OnStop is executed, then A's OnDestroy is executed, because the current task has no activity,a the importance of the process is reduced to very low, Soon A's process is closed by the system.
Go Android Life cycle