In The Android system, all applications are composed of four components provided by the system, namely Activity, Service, ContentProvider, BroadcastReceiver. Before, they cooperate with each other through Intent. As the carrier of the page, Activity is also the most frequent component in our daily development, and it is also the component we need to master the most.
The Activity life cycle
In the Android system, the life cycle is used to describe the switch between different states of components, and the callback of components in different life cycles is used to achieve the acquisition and release of resources. Therefore, it is of great significance to master the life cycle for memory management and the implementation of daily business. The diagram shows the full life cycle of an Activity.
In an Activity, the life cycles are onCreate, onStart, onResume, onPause, onStop, and onDestory. The different life cycles represent different states of the Activity:
- OnCreate: Creation of the Activity
- OnStart: starts the Activity
- OnResume: Resumes the Activity
- OnPause: Pauses the Activity
- OnStop: Stops the Activity
- OnDestory: Destruction of Activity
In fact, it is not difficult to find an Activity’s life cycle in pairs, which can be divided into three types: complete, visual, foreground:
- Complete life cycle: The full life cycle of an Activity from creation to destruction, i.e. onCreate to onDestory, should be noted that although onDestory is the Activity’s destruction callback method, its callback timing is not guaranteed. For example, the Activity may be released when memory runs out. This will be enforced whether you like it or not, but the system also provides us with a way to recover the state accordingly.
- Visual life cycle: Activities can be displayed on the screen but not interacted with. The life cycle is between onStart and onStop. OnStart and onStop may be called multiple times throughout the Activity’s life cycle.
- The foreground life cycle: An Activity is at the top of the stack, where it gets focus, has the highest resource usage, and also has user input and interaction. The foreground life cycle is between onResume and onPause.
These three life cycles are nested layer by layer to correspond to the changes of different life cycles. When an Activity is recycled due to system reasons, three special life cycles will be involved: OnRestart, onSaveInstanceState, and onRestoreInstanceState. OnRestart is called when the Activity resumes from the stack. OnSaveInstanceState is used to store various states of the current Activity to prepare for recovery after the Activity is recovered abnormally. OnRestoreInstanceState is used to restore Acitivy after the Activity is recovered abnormally.
Call timing of the Activity lifecycle
Activity Jumps between pages
If you have two activities (A and B), the corresponding life cycle is as follows when you jump from A to B:
- A.onCreate
- A.onStart
- A.onResume
- A.onPause
- B.onCreate
- B.onStart
- B.onResume
- A.onStop
It is not difficult to find that after calling the pause life cycle function of A, the startup process of B will be executed. Therefore, if there are time-consuming tasks in the pause life cycle of A, the startup delay of B will be caused. Therefore, we prohibit the existence of time-consuming methods in the pause method. And the life cycle after we hit the return key:
- B.onPause
- A.onStart
- A.onResume
- B.onStop
- B.onDestory
Whether it’s a jump or a pop-up page, the Activity will always suspend the Activity that is about to lose focus and then create or restore the Acitivy that is about to gain focus. The lifecycle changes when the Activity we start is transparent:
- A.onCreate
- A.onStart
- A.onResume
- A.onPause
- B.onCreate
- B.onStart
- B.onResume
Lifecycle triggered by page configuration
Life cycle changes can also occur when the page configuration changes, such as when our screen rotates or the screen size changes, which actually destroys the current Activity and creates a new one:
- onCreate
- onStart
- onResume
- onPause
- onStop
- onSaveInstanceState
- onDestroy
- onCreate
- onStart
- onRestoreInstanceState
- onResume
Among them, onRestoreInstanceState and onSaveInstanceState, two special life cycles, are used to store and restore states. When we are in the Activity configuration android: configChanges = “orientation” | screenSize “will not be destroyed after the reconstruction of the Activity, will only callback onConfigurationChanged method.
Summary: onStop is called when the activity loses focus and is not visible, and onPause is called when the activity loses focus and is visible. OnSaveInstanceState must handle the state of the activity, otherwise it may cause a bad experience for the user. The life cycle of an Activity switches by pausing to process the Activity that is about to gain focus, and then processing the subsequent life cycle of the Activity that has just lost focus.