Let’s start with the official Android API screenshot oncreate()->onstart()->onResume()->onRestart()->onPouse()->onStop()->onDestory()
The four states of an Activity
1 Running Status
When an Activity is in the foreground, that is, at the top of the stack, it is active or running.
2 Pause Status
An Activity is paused when it loses focus but is still visible (for example, a dialog box pops up on an Activity that does not fill the screen; the original Activity is still visible to the user). A suspended Activity is still fully alive (it saves the previous state and member variables), but is killed by the system if memory is low.
3 Stop Status
But when an Activity is completely hidden by another Activity, that Activity is stopped (that is, it is no longer at the top of the stack and is not visible to the user). A stopped Activity saves its previous state and member variables, but can be killed by the system if it runs out of memory (which is more likely than a paused Activity).
4 Destruction state
When an Activity is removed from the return stack, it is destroyed. Activities in the destroyed state are most likely to be reclaimed by the system.
The Activity’s seven callback methods
1 onCreate: Create stands for create, which is the first method in the Activity lifecycle and the most common lifecycle method we encounter in Android development. It does some initialization of the Activity itself, such as loading the layout with setContentView, initializing controls and variables, etc. But there are a lot of people who put a lot of code here that doesn’t have anything to do with initialization, and it’s actually not normative. At this point the Activity is still in the background and not visible. So the animation should not be initialized here because you can’t see…
2 onStart: Start means to start, which is the second method in the Activity lifecycle. At this point the Activity is visible, but not yet in the foreground, so we can’t see it and interact with it. There is nothing wrong with putting the initialization of the Activity in onCreate because of the official recommendation and the custom we developed.
3 onResume: Resume means to continue, to start again, the name and its duties are the same. The Activity is now ready to start after the first two phases of initialization. The Activity is already in the foreground and visible at this stage. The exclusive device can be turned on at this stage
4 onPause: Pause is used to pause an Activity when it wants to jump to another Activity or when the application exits normally. At this point, the Activity is visible in the foreground, so we can do some light-weight work of storing data and deinitializing it. It’s not too time-consuming, because when we jump an Activity, another Activity starts only after one Activity has finished executing onPause. And android specifies that if the onPause is not finished within 500ms, that is, 0.5 seconds, it will force the Activity to stop. You can see from the lifecycle diagram that it is possible to restart quickly here, but this is actually rare, such as when a user presses the back key on the way to the next Activity and quickly cuts back.
5 onStop: Stop indicates to stop the Activity. At this point, the Activity is no longer visible, but the Activity object is still in memory and has not been destroyed. The main work at this stage is also to recycle some resources.
6 onDestroy: Destroy indicates that the Activity is destroyed and invisible, so we can release resources that have not been released and do some recycling.
7 onRestart: restart means to restart the Activity. This method is triggered when the user presses the Home button to switch to the desktop and then cuts back or from the previous Activity to the previous Activity. You don’t normally do anything here.
Three life cycles for an Activity
The activity has three life cycles, which can be seen in the figure above.
Complete survival
The full life of an Activity is the time between the first call to the onCreate() method and the last call to the onDestroy() method;
Initialization should be done in the onCreate() method and memory freed in the onDestroy() method.
Visible survival
The visible lifetime of an Activity is the time between the onStart() method being called and the onStop() method being called;
During the visible lifetime, an Activity is visible, even if the Activity is no longer in the foreground and cannot interact with users;
The onStart() and onStop() methods may be called multiple times during the Activity’s life cycle as the Activity alternates between being visible and hidden from the user.
Foreground life
The foreground lifetime of an Activity is the period between when onResume() is called and when onPause() is called.
During the foreground life, the Activity is running, on top of all other activities on the screen, and can interact with the user.
The main concern here should be the order in which activities switch.
(A)onPause→(B)onCreate→(B)onStart→(B)onResume→(A)onStop
(A)onPause→(A)onStop→(B)onCreate→(B)onStart→(B)onResume
(1) An Activity can take up more or less system resources, and in the official proposal, onPause frees up a lot of system resources, providing a smooth transition between activities without having to wait two more phases, which makes switching faster.
(2) If the user switches back to the original Activity, the onResume method is called directly after the onPause method. This is much faster than onPause, onStop, onRestart, onStart, onResume.
As an example to help remember, when we edit a text message before sending it, and then switch to the desktop, the text message is not destroyed.
We can use the code of the official document to write a demo to help us remember. Thank you for your attention. Let’s exchange and learn together if you like.