Three cycles

Provide two diagrams of the Activity lifecycle model to help you understand:

 

Figure 1

 

 

Figure 2

As you can see from the Activity lifecycle shown in Figure 2, there are two layers of cycles in this diagram,

The first loop is onPause -> onResume -> onPause,

The second loop is onStop -> onRestart -> onStart -> onResume -> onPause -> onStop. We can think of these two layers as sub-lifecycles within the integrated Activity lifecycle. The first cycle is called the focus life cycle and the second is called the visual life cycle. That is, the first layer of the loop loops as the Activity gains and loses focus, and the Activity is always visible during this process. The second loop loops between Activity visibility and invisibility, with the Activity’s focus gaining and losing. That is, the Activity is first displayed, then gains focus, then loses focus, and finally makes the current Activity invisible as other activities pop up. Therefore, an Activity has three life cycles:

  1. Overall life cycle: onCreate ->… . – > onDestroy.
  2. Visual lifecycle: onStop ->… . – > onPause.
  3. Focus lifecycle: onPause -> onResume.

Four stages

The seven lifecycle methods above are called in a certain order in four phases as follows:

  1. Start Activity: Executes three lifecycle methods in this phase: onCreate, onStart, and onResume.
  2. Activity loses focus: If you enter another Activity or application while the Activity has gained focus, the current Activity loses focus. At this stage, the onPause and onStop methods are executed in sequence.
  3. Activity regains focus: If the Activity regains focus, three lifecycle methods are executed in sequence: onRestart, onStart, and onResume.
  4. Stopping an Activity: When an Activity is stopped, the system executes three lifecycle methods: onPause, onStop, and onDestroy.

If no state change occurs during the execution of the lifecycle methods in each of the four phases, the system will execute the lifecycle methods as described above, but if the state changes during execution, the system will invoke the lifecycle methods in a more complex manner.

The lifecycle methods that can change the system’s execution trajectory during execution are onPause and onStop. If the Activity regains focus during the execution of onPause, then loses focus again. Instead of executing the onStop method, the system will execute the corresponding lifecycle methods in the following order:

onPause -> onResume-> onPause

If the Activity regains focus during the execution of the onStop method, then it loses focus again. Instead of executing onDestroy, the system will execute the corresponding lifecycle methods in the following order:

onStop -> onRestart -> onStart -> onResume -> onPause -> onStop

As you can see in the Activity lifecycle shown in Figure 2, the system calls the onPause, onStop, and onDesktroy methods when terminating the application process. The onPause method comes first, meaning that the Activity may be terminated if it loses focus, while the onStop and onDestroy methods may not have a chance to execute. Therefore, you should save the current Activity state in the onPause method so that the code that saves the Activity state can be executed at any time you terminate the process.

 

Seven methods

They are explained in detail below (the boldface text indicates that the concepts that will be used frequently in the following text will be explained when they first appear, and will not be explained in detail thereafter) : \

1. Void onCreate(Bundle savedInstanceState) is executed when the Activity is first loaded. When we start a new application, the onCreate event on its main form will be executed. If an Activity is reloaded into a Task after onDestroy, its onCreate event is also re-executed. Notice that the savedInstanceState parameter (the Bundle type is a set of key-value pairs, you can view it as. Net Dictionary is a very useful design. Because of the particularity of mobile applications mentioned above, an Activity can be forcibly swapped to the background (swapped to background means that the form is no longer visible to the user, but still exists in a Task). For example, a new Activity “covers” the current Activity by pressing the current Task, or the user presses the Home button to return to the desktop, or some other important event occurs that causes a new Activity to appear on top of the current Activity, such as the incoming call screen), If the user does not revisit the form after a period of time (Android can select the last 6 programs by holding down the Home button, or the user can directly click the program’s run icon again, if the Task and process of the form are not destroyed by the system, there is no need to reload the form. Redisplaying the Activity at the top of the Task is called reviewing an application’s form). The form, along with its Task and Process, may have been destroyed by the system, and if you view the form again, you need to re-initialize the form with the onCreate event. At this point, we might want the user to continue operating as they did when they last opened the form, rather than starting from scratch. For example, when a user is editing an SMS message, he or she suddenly receives an incoming call. After answering the call, the user does something else, such as saving the calling number to a contact, but does not immediately return to the SMS editing interface, which results in the SMS editing interface being destroyed. When the user re-enters the SMS program, he or she may want to continue editing last time. In this case, we can override the Activity’s void onSaveInstanceState(Bundle outState) event by writing some state or information to the outState that we need to save before the form is destroyed, so that when the form re-executes onCreate, The previously saved information is passed in via savedInstanceState, at which point we can optionally use this information to initialize the form, rather than starting from scratch. 2. Void onStart() activity is called when the activity becomes visible to the user on the screen. Execute after the onCreate event. Or after the current form is swapped to the background, a period of time has passed before the user can view the form again, the form has executed the onStop event, but the form and its process have not been destroyed, the user can view the form again and execute the onRestart event, after which the onCreate event will be skipped. Execute the form’s onStart event directly. 3. Void onResume() activity is called when the activity starts interacting with the user (this method is always called when an activity is started or restarted). Executed after the onStart event. Or after the current form is swapped to the background, the user can view the form again without destroying the form or executing the onStop event (the form still exists in the Task). The onResume event is directly executed, skipping the onCreate and onStart events of the form. Void onPause() activity is called when the CPU and other resources are paused or recalled. This method is used to save the state of the activity, but also to protect the scene. Executed when the form is swapped to the background. 5. Void onStop() Activity is called when the activity is stopped and turns into an invisible phase and subsequent lifecycle events. Executed after the onPause event. If the user has not revisited the form for a period of time, the form’s onStop event will be executed; Or if the user simply presses the Back key to remove the form from the current Task, the form’s onStop event is executed. 6. Void onRestart() is invoked when the activity is restarted. Instead of starting a new activity, the activity remains on the stack. After the onStop event is executed, if the window and its process are not destroyed and the user views the window again, the onRestart event of the window is executed. After the onRestart event, the onStart event is directly executed, bypassing the onCreate event of the window. 7. Void onDestroy() activity is called when it is completely removed from system memory. This method is called either because someone called onFinish() directly or because the system decided to stop the activity to free resources! The Activity is executed when it 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 an Activity. Suppose A program consists of two activities A and B. A is the program’s launch screen. When the user starts the program, Process and the default Task are created, and A is pressed into the current Task. The onCreate, onStart, and onResume events are presented to the user. At this time, the user selects A function in A to start interface B. Interface B is pressed into the current Task to cover the execution of THE onPause event of A, and the execution of the onCreate, onStart, and onResume events of B, presenting interface B to the user. After performing operations on interface B, you can press the Back key to return to interface A. Interface B is no longer visible. The onPause, onStop, and onDestroy events of interface B are executed and the onResume event of interface A is executed. When an incoming call is received, the onPause event of interface A is executed, and the call answering interface is displayed to the user. After answering the call, the user presses the Home button to return to the desktop, opens another program “Contacts”, adds contact information, and performs other operations. At this time, interface A is no longer visible, and the onStop event is executed. But it was not destroyed. The user then re-clicks our application from the menu. Since A and its processes and tasks are not destroyed, A’s onRestart and onStart events are executed, and A’s onResume events are executed, and A is presented to the user. After the user finishes using the Task, press the Back key to return to the desktop. A’s onPause and onStop are executed, and then A’s onDestroy is executed. Since there is no Activity in the Task, the importance of the Process in which A is involved is reduced to A low level. Soon, the Process of A was terminated by the system.

\