The state and state transition of the entire life cycle of the Activity and the corresponding callback method

The life cycle of Activity can be divided into the following 7 states:

  1. Created state (Created): When the Activity is called, it will enter the created state, and the Activity will execute the onCreate() callback method.
  2. Started state (Started): When the Activity is already visible but cannot be interacted with, the Activity will enter the Started state. At this time, the Activity will execute the onStart() callback method.
  3. Resumed state (Resumed): When the Activity is visible and can be interacted with, the Activity will enter the resumed state. At this time, the Activity will execute the onResume() callback method.
  4. Paused state (Paused): When the Activity loses focus or is partially blocked, the Activity will enter the Paused state, and the Activity will execute the onPause() callback method at this time.
  5. Stopped state (Stopped): When the Activity is not visible, the Activity will enter the stopped state, and the Activity will execute the onStop() callback method at this time.
  6. Destroyed state (Destroyed): When the Activity is destroyed, the Activity will enter the destroyed state. At this time, the Activity will execute the onDestroy() callback method.
  7. Restarted state (Restarted): When the Activity is restarted from the stopped state, the Activity will enter the restarted state. At this time, the Activity will execute the onRestart() callback method.

In addition to the above states, Activity can also enter the following two states:

  1. Early start state (Pre-Started): When the Activity has been created but has not yet entered the start state, the Activity will enter the early start state. At this time, the Activity will execute the onCreate() and onStart() callback methods.
  2. Visible-Paused state (Visible-Paused): When the Activity is blocked or loses focus, the Activity will enter the Visible-Paused state. At this time, the Activity will execute the onPause() and onStop() callback methods.

The state transitions are as follows:

  1. From the created state (Created) to the started state (Started): call the onStart() method
  2. From the Started state (Started) to the resumed state (Resumed): call the onResume() method
  3. From the resumed state (Resumed) to the paused state (Paused): call the onPause() method
  4. From the paused state (Paused) to the resumed state (Resumed): call the onResume() method
  5. From the resumed state (Resumed) to the stopped state (Stopped): call the onStop() method
  6. From the stopped state (Stopped) to the resumed state (Resumed): call the onRestart() and onStart() methods
  7. From the stopped state (Stopped) to the destroyed state (Destroyed): call the onDestroy() method

The callback method is as follows:

  1. onCreate(): The method executed when the Activity is created, some initialization operations can be performed in this method, such as binding layout, setting interface elements, etc.
  2. onStart(): The method executed when the Activity enters the visible state, and some UI initialization and other operations can be performed in this method.
  3. onResume(): The method executed when the Activity enters the interactive state of the foreground. In this method, some operations such as data loading, audio playback and resource application can be performed.
  4. onPause(): The method executed when the Activity loses focus. In this method, some operations such as resource release and file saving can be performed.
  5. onStop(): The method executed when the Activity enters the invisible state in the background. In this method, some operations such as releasing and pausing resources can be performed.
  6. onRestart(): The method executed when the Activity is restarted, and some data recovery operations can be performed in this method.
  7. onDestroy(): A method that is executed when the Activity is destroyed. In this method, some operations such as resource release and file closing can be performed.

Guess you like

Origin blog.csdn.net/m0_52537869/article/details/131029944