In the last article, I introduced to you the use of buttons to display time in the android development series. Interested friends can click to read the details.
In the process of Activity from creation to destruction, 7 life cycle methods need to be called at different stages. These 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 certain order in 4 stages.
1, start Activity: At this stage, 3 life cycle methods are executed in sequence, namely onCreate, onStart, onResume
2, Activity loses focus: If you enter another Activity or application while the Activity gains focus, the current Activity will lose focus. At this stage, the onPause and onStop methods will be executed in sequence
3. Activity regains focus: At this time, the onRestart, onStart, and onResume methods will be executed in sequence
4. Close Activity: When the Activity is closed, the system will execute 3 life cycle methods in sequence, namely onPause, onStop, onDestory
If the state does not change during the execution of the life cycle methods in these four stages, the system will execute the life cycle methods in the four stages in sequence in the order above. If the state changes during the execution status, the system will call the life cycle method according to a more complex method
If the Activity regains the focus during the execution of the onStop method, and then loses the focus, the system will not execute the onDestory method, but as follows Sequential execution of life cycle methods
onStop->onRestart->onStart->onResume->onPause->onStop
The following figure describes the activity from creation to destruction and intermediate states The process of calling life cycle methods after changes
It is not difficult to see from the Activity life cycle calling method diagram shown above that the entire Activity life cycle contains two layers of loops. The first layer of loops is onPause- >onResume->onPause, the second layer loop onStop->onRestart->onStart->onResume->onPause->onStop. We can regard these two levels of cycles as sub-life cycles in the entire Activity life cycle.
The first-level loop is called the focus life cycle, and the second-level loop is called the visual life cycle. That is to say, the first-level loop cycles through the process of gaining and losing Activity focus. During the process, Activity is always visible. The second layer of loop is the loop in the process of Activity being visible and invisible. In this process, Activity focus is gained and lost. In other words, Activity will be displayed first, and then it will be displayed. Gain focus, then lose focus, and finally the current activity becomes invisible due to other activities popping up.
Therefore, Activity has the following three life cycles:
1. Overall life cycle: onCreate->....->onDestroy
2. Visual Life cycle: onStart->....->onStop
3. Focus life cycle: onResume->....->onPause
The following codes are in Activity Output log information in the 7 life cycle methods in the class
package com.neil.ad02; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d("onCreate","onCreate Method is executed"); setContentView(R.layout.activity_main); } @Override protected void onDestroy() { super.onDestroy(); Log.d("onDestroy","onDestroy Method is executed"); } @Override protected void onPause() { super.onPause(); Log.d("onPause","onPause Method is executed"); } @Override protected void onRestart() { super.onRestart(); Log.d("onRestart","onRestart Method is executed"); } @Override protected void onResume() { super.onResume(); Log.d("onResume","onResume Method is executed"); } @Override protected void onStart() { super.onStart(); Log.d("onStart","onStart Method is executed"); } @Override protected void onStop() { super.onStop(); Log.d("onStop","onStop Method is executed"); } }
Open Android Device Monitor to observe
The red box in the picture is to regain focus. You can try the others one by one.
The above content is the life cycle of window activity in the second part of the Android learning series introduced by the editor. I hope it will be helpful to everyone!
For more articles related to the life cycle of window Activity in Android Development Series 2, please pay attention to the PHP Chinese website!