Understanding the activity life cycle

Understanding the activity life cycle

A cycle is the various states an activity goes through from the beginning to the end. The life cycle is the various states that an activity goes through from the beginning to the end. The transition from one state to another, from nothing to something and then to nothing, is called a life cycle.

  • Description of each life cycle state
  • normal life cycle
  • The difference between onPause and onStop

Description of each life cycle state

onCreate: Paired with onDestroy, indicating that the Activity is being created, which is the first method of the life cycle. In this method, you can do some initialization work (loading layout resources, initializing data required for Activity, etc.), and time-consuming work is completed on the asynchronous thread.

onRestart: Indicates that the Activity is being restarted. Under normal circumstances, onRestart will be called when the current Activity changes from invisible to visible again. This situation is generally caused by the user's behavior, such as the user pressing the Home button to switch to the desktop or opening a new Activity (at this time the current Activity will be suspended, that is, onPause and onStop are executed), and then the user will respond When it comes to this Activity, this situation will occur.

onStart: Paired with onStop, indicating that the Activity is being started and is about to start. But at this time, you should pay attention to the difference between it and onResume. Both indicate that the Activity is visible, but at onStart the Activity is loading other content, which is being displayed to us, and the user cannot see it yet, that is, it cannot interact.

onResume: Paired with onPause, it means that the Activity has been created and can start the activity. At this time, the user can already see the interface and is about to interact with the user (after completing this cycle, the user's interaction event can be responded to).

onPause: Paired with onResume, it means that the Activity is suspended. Under normal circumstances, onStop will be called next. In special cases, if the user quickly returns to the current Activity at this time, then onResume will be called (extreme case). Generally speaking, in this life cycle state, you can do some work of storing data and stopping animations, but it should not be too time-consuming. If this state is awakened due to starting a new Activity, it will affect the display of the new Activity. The reason is that onPause must be executed before onResume of the new Activity will be executed.

onStop: Paired with onStart, it means that the Activity is about to stop, and some heavyweight recycling work can be done, which is also not too time-consuming (it can be slightly better than onPause).

onDestroy: Paired with onCreate, it means that the Activity is about to be destroyed. This is the last callback of the Activity life cycle. We can do some recycling work and final resource release (such as Service, BroadReceiver, Map, etc.).

normal life cycle

Under normal circumstances, the common life cycles of Activity are the above 7. The following figure describes the switching process of various life cycles in more detail
Write picture description here
onStart and onStop, onResume and onPause are paired.
There are two ways for Activity to return to the foreground. Returning to the foreground from the onPause state will go to the onResume state. Returning to the foreground from the onStop state will go to the onStart state. This depends on whether it is visible and whether it is in the foreground. To say.
In terms of whether they are visible, onStart and onStop are paired; in terms of whether they are in the foreground, onResume and onPause are paired

The difference between onPause and onStop

I believe many people are confused about the difference between onPause and onStop

1. Under normal circumstances, start FristActivity, jump from FristActivity to SecondActivity, and then return to FristActivity. The life cycle is as follows:

FirstActivity  ---> onCreate
FirstActivity  ---> onStart
FirstActivity  ---> onResume   //FristActivity启动完毕
FirstActivity  ---> onPause
SecondActivity ---> onCreate
SecondActivity ---> onStart
SecondActivity ---> onResume
FirstActivity  ---> onStop     //跳转到SecondActivity
SecondActivity ---> finish
SecondActivity ---> onPause
FirstActivity  ---> onStart
FirstActivity  ---> onResume
SecondActivity ---> onStop
SecondActivity ---> onDestroy  //返回到FristActivity

2. If SecondActivity is DialogActivity (add android:theme="@style/Theme.AppCompat.Dialog" to AndroidManifest), the operation and other configurations are the same, and the life cycle is as follows:

FirstActivity  ---> onCreate
FirstActivity  ---> onStart
FirstActivity  ---> onResume   //FristActivity启动完毕
FirstActivity  ---> onPause
SecondActivity ---> onCreate
SecondActivity ---> onStart
SecondActivity ---> onResume   //跳转到SecondActivity
SecondActivity ---> finish
SecondActivity ---> onPause
FirstActivity  ---> onResume
SecondActivity ---> onStop
SecondActivity ---> onDestroy  //返回到FristActivity

There are fewer FirstActivity —> onStop and FirstActivity —> onStart cycles than in case 1.

3. If SecondActivity is a normal Activity, quickly click back when jumping from FristActivity to SecondActivity, and the life cycle will be the same as in 2.

Guess you like

Origin blog.csdn.net/chenxiabinffff/article/details/80770207