Activity lifecycle conclusion

  • Start Activiy: onCreate => onStart() => onResume() and the Activity enters the running state.
  • The Activity exits the background (Home or starts a new Activity): onPause() => onStop().
  • The Activity returns to the foreground: onRestart() => onStart() => onResume().
  • If the Activity is running out of memory during the background, the startup process is restarted when it is started again.
  • Lock screen: onPause() => onStop().
  • Unlock: onStart() => onResume().

I met the Activity

In daily applications, an Activity is an interface to interact with users. It provides a window for users to complete relevant operations. When an Activity is created in development, the setContentView(View) method is called to specify a layout interface for the Activity, which provides the interface for user interaction. Android system is through the Activity stack to manage activities, and the Activity itself is through the lifecycle method to manage its creation and destruction, so now let’s look at how the Activity lifecycle works.

The Activity of the form

Active/Running

When an Activity is active, it is visible at the top of the stack and can interact with the user.

Paused

The Activity transitions to Paused when it loses focus, is placed at the top of the stack by a new Activity that is not full-screen, or by a transparent Activity. However, we need to understand that the Activity only loses the ability to interact with the user, all its state information and its member variables are still present, and can only be reclaimed by the system if the system is running out of memory.

Stopped

When an Activity is completely overwritten by another Activity, the covered Activity goes into the Stopped state, where it is no longer visible but holds all of its state information and its member variables as Paused.

Killed

When an Activity is discarded by the system, it is Killed. The Activity switches between these four modes, and how it switches varies depending on the user. Now that you know about the four forms of an Activity, let’s talk about the Activity lifecycle.

Activity Lifecycle

Let’s start with a classic life cycle flow chart:

Typical lifecycle approach

onCreate

This method is called back when the Activity is created. It is the first method called in the lifecycle. When creating an Activity, we usually need to override this method, and then do some initialization operations in this method, such as setting the resources of the interface layout through setContentView, and initializing the required component information.

onStart

This method is called back to indicate that the Activity is being started and that the Activity is already visible, but not yet displayed in the foreground, so it cannot interact with the user. The Activity is displayed and we can’t see the pendulum.

onResume

When this method is called back, the Activity is visible in the foreground and ready to interact with the user. The similarity between onResume and onStart is that both methods indicate that the Activity is visible, but the onStart callback is still in the background and cannot interact with the user. OnResume is displayed in the foreground and can interact with the user. The onResume method is also called when the Activity stops (onPause and onStop are called) and comes back to the foreground, so we can also initialize some resources in the onResume method. Such as reinitializing resources released in onPause or onStop.

onPause

This method is called back to indicate that the Activity is stopped (Paused), and normally the onStop method will be called back soon after. If the onResume method is immediately executed after the onPause method is executed, the onResume method will be called back to the current Activity. Of course, we can do some data storage or animation stops or resource reclaiming in onPause, but this should not be too time-consuming, as it may affect the display of the new Activity — the onResume method of the new Activity will not be executed until the onPause method is finished.

onStop

When the Activity is Stopped or completely overwritten, it is not visible and only runs in the background. Similarly, the onStop method allows you to do some resource release (not too time-consuming).

onRestart

Indicates that the Activity is being restarted. This method is called back when the Activity changes from invisible to visible. In this case, when the user starts a new Activity, the current Activity is paused (onPause and onStop are executed), and when the user returns to the current Activity page, the onRestart method is called back.

onDestroy

At this point the Activity is being destroyed and is the last method to execute in the lifecycle, so we can usually do some recycling and the final resource release in this method.

Activity Exception life cycle

An abnormal life cycle is when an Activity is reclaimed by the system or the Configuration of the current device changes, causing the Activity to be destroyed and rebuilt. The life cycle of exceptions can be divided into the following two situations:

  1. Related system configuration changes cause the Activity to be killed and recreated.
  2. Low priority activities are killed due to insufficient memory

So what do the onSaveInstanceState and onRestoreInstanceState methods do?

In fact, these two methods are called automatically by the system. When the system configuration changes, the Activity is destroyed. That is, onPause, onStop, and onDestroy are called in sequence. Android automatically calls the onSaveInstanceState method to store the state of the current Activity, so you can store some data in the onSaveInstanceState method so that you can restore the data after the Activity is rebuilt. Of course, this method must be called before the onStop method, before the Activity stops. Call the onPause method at any time.

When the Activity is recreated, the onRestoreInstanceState method is also called, And pass the onRestoreInstanceState and onCreate methods the same Bundle object that was saved by onSaveInstanceState when the Activity was destroyed. Therefore, we can use the onRestoreInstanceState and onCreate methods to determine whether the Activity has been recreated. If it has been rebuilt, we can restore the previous data. The onRestoreInstanceState method is called after onStart.

The onSaveInstanceState and onRestoreInstanceState methods are only called when the Activity terminates abnormally. They are normally not called.

How to select onRestoreInstanceState and onCreate for data restoration?

The difference between onRestoreInstanceState and onCreate is that once the onRestoreInstanceState method is called back, its Bundle must not be empty, so you don’t need to make any extra checks, whereas onCreate’s Bundle may not have a value, because if the Activity is started normally, The Bundle argument will not have a value, so we need additional criteria. Of course, both methods can restore data, but the onRestoreInstanceState method is preferred.