Android Activity Lifecycle

Android uses the task stack (TASK) to manage activities. A task is a collection of activities stored in a stack (first in, last out), which is also known as the return stack.

There are 4 states in the Android activity lifecycle.

1. Running status

An activity is in the running state when it is at the top of the return stack.

Recycling activities that are in the running phase will bring a very poor user experience

2. Pause state

An activity enters the paused state when it is no longer at the top of the stack, but is still visible.

The activities in the suspended state are still completely alive, and the system is not willing to recycle the activities during this state, and the user experience is not good (still visible). Only in the case of extremely low memory, the system will consider recycling this Activity

3. Stop the activity

When an activity is not in the top state of the stack, and safety is not visible, it enters the stopped state. The system will still save the corresponding state and member variables for this activity, but this is not completely reliable. When memory is required elsewhere , activities in the stopped state may be recycled by the system.

4. Destruction state

When an activity is removed from the return stack, it becomes a destroyed state, and the system will most likely recycle activities in this state to ensure sufficient memory on the phone.


activity lifetime

Seven callback methods are defined in the Activity class, covering every link of the activity life cycle.

1.onCreate()

It is called when the activity is first created, and the initialization of the activity should be done in this method, such as loading the layout, binding events, etc.

2.onStart()

This method is called when the activity changes from invisible to visible

3.onResume ()

This method is called when the activity is ready to interact with the user

4.onPause()

This method is called when the system is ready to start or resume another activity. We usually release some CPU-consuming resources and save some key data in this method.

The execution speed of this method must be fast, otherwise it will affect the user experience

5.onStop ()

This method is called when the activity is completely invisible (not in a paused state). The main difference between it and onPause() is that if the new activity started is a dialog-like activity, the onPause() method will be executed, while The onStop() method will not be executed. The reason, if the dialog is destroyed, it will affect the user experience,

6.onDestroy ()

This method is called before the activity is destroyed, after which the state of the activity will change to the destroyed state.

7.onRestart()

This method is called before the activity changes from the stopped state to the running state, i.e. the activity is restarted.


Except for the onCreate() method, the above 7 methods are all relative to each other, so that the activities can be divided into 3 types of lifetimes.

Complete lifetime:

What an activity experiences between onCreate and onDestroy methods is the complete lifetime.

Generally, an activity will complete various initialization operations in the onCreate method, and will complete the memory release operation in the onDestroy method.

Visible lifetime

What an activity experiences between onStart and onStop methods is the visible lifetime.

We can use these two methods to reasonably manage which resources are visible to the user. For example, resources are loaded in onStart, and resources are released in onStop method, so as to ensure that activities in the stopped state will not occupy too many resources.

foreground lifetime

What is experienced between the onResume method and the onPause method is the foreground lifetime.

In the foreground lifetime, the activity is always in the running state, and the activity at this time can interact with the user. We usually see and contact the activities in this state.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325762086&siteId=291194637