First, let's start with the classic image provided by Google, which is an intuitive representation of the navigation transitions between the various stages of the Activity lifecycle.
The Activity class provides six core callbacks: onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy().
Explanation#
- Under normal circumstances, an Activity goes through the entire lifecycle in the following order: onCreate() -> onStart() -> onResume() -> onPause() -> onStop() -> onDestroy().
- onCreate(): Triggered when the system first creates the Activity. It can be used for initialization work, such as initializing data required by the Activity and calling setContentView to load the layout resources.
- onRestart(): Indicates that the Activity is being restarted. Generally, when the current Activity transitions from invisible to visible, onRestart is called. This usually occurs due to user actions, such as pressing the Home button to switch to the home screen or opening another new Activity, and then returning to this Activity.
- onStart(): Indicates that the Activity is being started and is about to begin. At this point, the Activity has appeared, but it is not yet in the foreground and cannot interact with the user. At this point, the Activity can be understood as being displayed, but we still can't see it.
- onResume(): Indicates that the Activity is already visible, appearing in the foreground, and starting to operate. It needs to be compared with onStart(). When onStart() is called, the Activity is still in the background, and when onResume() is called, the Activity is displayed in the foreground.
- onPause(): Indicates that the Activity is being paused but still visible. Normally, onStop will be called immediately after onPause. No time-consuming operations should be performed in onPause, as it will affect the display of the new Activity. onPause must be executed before the new Activity's onResume is executed.
- onStop(): Indicates that the Activity is about to stop and will become invisible and move to the background. Some slightly heavyweight cleanup work can be done here, but it should not be too time-consuming.
- onDestroy(): Indicates that the Activity is about to be destroyed. This is the last callback in the Activity lifecycle and can be used for cleanup work and final resource reclamation.
Common Scenarios#
Jumping from Activity A to Activity B, and then closing Activity B to return to Activity A#
- For the launched Activity B, the first time it is started, the callbacks are as follows: onCreate() -> onStart() -> onResume()
- When the user opens Activity B, the callback for Activity A being invisible is as follows: onPause() -> onStop()
- When returning from Activity B to the original Activity A, the callback for Activity A going from invisible to visible is as follows: onRestart() -> onStart() -> onResume()
- When pressing the back button to go back, the callbacks for Activity B are as follows: onPause() -> onStop() -> onDestroy()
- When switching to the home screen and then returning to Activity A, the callbacks are as follows: onPause() -> onStop() -> onRestart() -> onStart() -> onResume()
- After calling the finish() method, the callbacks are as follows: onDestroy() (taking the example of calling it in the onCreate() method, callbacks may differ in different methods, but usually it is called in the onCreate() method)
Three Running States of an Activity#
Resumed State#
Also known as the Running state, this Activity is currently displayed on the screen and has user focus. This is the Activity that the user is currently interacting with.
Paused State#
This is a less common state. The Activity is visible on the screen but is not the foremost Activity. For example, there may be another non-fullscreen or transparent Activity in the Resumed state that does not completely cover this Activity.
Stopped State#
When the Activity is completely invisible, it is still running in the background and its state is retained in memory. It is not completely destroyed. This is easy to understand. When switching to another screen, the previous screen is still running in the background. Pressing the back button will restore the previous state. Most software, when opened, directly presses the Home button without closing it. At this time, the Activity is in the Stopped state.
Partial Flow Branches#
- Starting an Activity: onCreate() -> onStart() -> onResume(), the Activity enters the running state.
- Activity goes to the background: When the current Activity transitions to a new Activity or the Home button is pressed to return to the home screen: onPause() -> onStop(), entering the stopped state.
- Activity returns to the foreground: onRestart() -> onStart() -> onResume(), returning to the running state.
- When the Activity is in the background and the system is low on memory, the system may kill the Activity in the background (at this time, the reference to this Activity is still in the task stack, but the reference points to null). If you return to this Activity again, the lifecycle will be: onCreate() -> onStart() -> onResume() (the initialization lifecycle of the Activity will be repeated).
- Lock screen: onPause() -> onStop()
- Unlock screen: onStart() -> onResume()
Switching between Landscape and Portrait#
First case, destroying the current Activity:#
During the process of switching between landscape and portrait, the Activity is destroyed and then recreated. This situation should also be avoided.
There are two callbacks involved:
- onSaveInstanceState and onRestoreInstanceState.
- When an Activity is terminated due to exceptional circumstances, the system calls onSaveInstanceState to save the state of the current Activity. This method is called before onStop, and it has no predetermined sequence relationship with onPause. This method is only called when the Activity is terminated due to exceptional circumstances. After the terminated Activity is rebuilt, the system calls onRestoreInstanceState and passes the Bundle object parameter saved by the onSaveInstanceState method when the Activity was destroyed to both onRestoreInstanceState and onCreate methods. Therefore, the Activity's state can be restored through the onRestoreInstanceState method. The timing of this method call is after onStart. The difference between onCreate and onRestoreInstanceState in restoring the state of the Activity is that onRestoreInstanceState callback indicates that the Bundle object is not empty, so no null check is required. onCreate requires a null check. It is recommended to use onRestoreInstanceState.
- In summary, the lifecycle of the Activity during this process is: onPause() -> onSaveInstanceState() -> onStop() -> onDestroy() -> onCreate() -> onStart() -> onRestoreInstanceState() -> onResume()
Second case, the current Activity is not destroyed, but we need to set the Activity's attributes:#
You can specify the following attributes in the Activity in the AndroidManifest file:
<activity
android:name=".activity.VideoDetailActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"/>
To avoid the destruction and recreation of the Activity when switching between landscape and portrait, you can use the following callback method:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
Priority Order of Retaining Activities When Resources Are Insufficient:#
- Foreground Activity: The Activity that is currently interacting with the user has the highest priority.
- Visible but not Foreground Activity: For example, if a dialog is displayed in an Activity, causing the Activity to be visible but not in the foreground and unable to interact with the user.
- Background Activity: An Activity that has been paused, such as when onStop is called, has the lowest priority.