First, let's look at the classic diagram provided by Google, which provides an intuitive representation of the navigation transitions between different 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(): This callback is triggered when the Activity is first created by the system. It can be used to perform initialization work, such as initializing data required by the Activity and calling setContentView to load the layout resources.
- onRestart(): This callback 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 the original Activity.
- onStart(): This callback 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. It can be understood that the Activity has been displayed, but we still cannot see it.
- onResume(): This callback 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(): This callback indicates that the Activity is being stopped but is still visible. Normally, onStop() will be called immediately after onPause(). It is not recommended to perform time-consuming operations in onPause() because it will affect the display of the new Activity. onPause() must be completed before the new Activity's onResume() is executed.
- onStop(): This callback 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(): This callback indicates that the Activity is about to be destroyed. It is the last callback in the Activity lifecycle and can be used for final cleanup and resource release.
Common scenarios:
Transition from Activity A to Activity B and then close 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, Activity A is in an invisible state, and the callbacks are as follows: onPause() -> onStop().
- When returning from Activity B to the original Activity A, Activity A transitions from invisible to visible, and the callbacks are 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, the callbacks may vary in different methods, but usually it is called in the onCreate() method).
Three states of an Activity:
Resumed (active 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 (paused state):#
This is a less common state. The Activity is visible on the screen, but it is not the foreground Activity. For example, there is another non-fullscreen or transparent Activity in the Resumed state that does not completely cover this Activity.
Stopped (stopped state):#
When the Activity is completely invisible, it is still running in the background and its state is retained in memory. This means that the Activity is not completely destroyed. This can be understood as when switching to another interface, the previous interface is still running in the background, and pressing the back button will restore the original state. Most software, when opened, directly presses the Home button without closing it, so the Activity is in the Stopped state.
Partial process branches:
- Starting an Activity: onCreate() -> onStart() -> onResume(), the Activity enters the running state.
- Activity goes to the background: When the current Activity switches to a new Activity interface or presses the Home button 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 the Activity is still in the task stack, but the reference points to a null object). If you return to this Activity again, the lifecycle callbacks will be as follows: 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 orientations:
First case, destroying the current Activity:#
During the process of switching between landscape and portrait orientations, the Activity is first destroyed and then recreated, and this situation should 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 does not have a predetermined sequence 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 is after onStart. The difference between onCreate and onRestoreInstanceState in restoring the Activity's state is that onRestoreInstanceState callback indicates that the Bundle object is not empty, so there is no need to check for null. 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 section of 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 orientations, you can use the following callback method:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
When resources are insufficient, the priority order for retaining Activities is as follows:
- 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, making the Activity visible but not able to interact with the user.
- Background Activity: An Activity that has been paused, such as when onStop is called, has the lowest priority.