In the process of activity from creation to destruction, it is necessary to call 7 life cycle methods at different stages, the 7 life cycle methods are defined as follows:
protected void onCreate (Bundle savedinstancestate) protected void OnStart () protected void Onresume () protected void OnPause () protected void onStop () protected void Onrestart () protected void OnDestroy ()
The above 7 life cycle methods are called in a sequence of 4 stages, respectively.
1. Start activity: Perform 3 life cycle methods in this phase, respectively, Oncreate,onstart,onresume
2,activity loses focus: If you enter another activity or application while the activity is focused, the current activity loses focus, and this phase executes the Onpause,onstop method in turn
3,activity regain Focus: The Onrestart,onstart,onresume method is executed sequentially
4, close activity: When activity is closed, the system executes 3 life cycle methods, namely Onpause,onstop,ondestory
If there is no state change during the 4-phase execution of the life cycle method, the system executes the 4-phase lifecycle method in the order above, and if the state is changed during execution, the system calls the life cycle method in a more complex way
If the activity re-receives the focus during the execution of the OnStop method and then loses focus, the system will not execute the Ondestory method, but the life cycle method is executed in the following order
Onstop->onrestart->onstart->onresume->onpause->onstop
Describes the process of invoking a life cycle approach from creation to destruction and the change in intermediate state.
It is not difficult to see from the activity life cycle as shown that there are two layers of loops throughout the activity life cycle, the first layer is onpause->onresume->onpause, and the second loop onstop-> Onrestart->onstart->onresume->onpause->onstop. We can consider these two layers of loops as the sub-lifecycles of the entire activity life cycle.
The first layer is called the Focus life cycle, and the second loop is called the visual life cycle, that is, the first layer of cyclic activity focuses on the acquisition and loss of the process of circulation, in this process, 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 activity focus, that is, the activity is first displayed, then the focus, then loses focus, Finally, the current activity becomes invisible because of the other activity that pops up.
Therefore, activity has the following 3 life cycles:
1. Overall life cycle: Oncreate->....->ondestroy
2. Visual life cycle: Onstart->....->onstop
3. Focus Life cycle: Onresume->....->onpause
The following code outputs the log information in 7 life cycle methods in the Activity class, respectively
Packagecom.neil.ad02;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); LOG.D ("OnCreate", "OnCreate Method is executed"); Setcontentview (R.layout.activity_main); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); LOG.D ("OnDestroy", "OnDestroy Method is executed"); } @Overrideprotected voidOnPause () {Super. OnPause (); LOG.D ("OnPause", "OnPause Method is executed"); } @Overrideprotected voidOnrestart () {Super. Onrestart (); LOG.D ("Onrestart", "Onrestart Method is executed"); } @Overrideprotected voidOnresume () {Super. Onresume (); LOG.D ("Onresume", "Onresume Method is executed"); } @Overrideprotected voidOnStart () {Super. OnStart (); LOG.D ("OnStart", "OnStart Method is executed"); } @Overrideprotected voidOnStop () {Super. OnStop (); LOG.D ("OnStop", "OnStop Method is executed"); }}
Open Android Device Monitor watch
The red box in the picture is the focal point again, and the others you can try
(Android Learning Series) two, window (Activity) life cycle