Activity [æk ɪ tɪvəti], Service [ˈs ɪ ːvɪs], BroadcastReceiver [ˈ p ɔː ː DK ɑːst] [rɪ siːvər], ContentProvider [kən ɪ tent] [prə vaɪdər]) is one of the most important activities in Android. It is mainly responsible for the display of the software interface. It is the face of the whole APP, and is the part that directly interacts with the user. So understanding the lifecycle of an Activity makes it easier to use it.

The lifecycle of an Activity

In general, there are seven main Activity lifecycles:

  • OnCreate: Indicates that the Activity is being created. This is the first method in the lifecycle. In this method, we do some initialization work, such as calling setContentView to load the interface layout resources, initializing data for the Activity, and so on.

  • OnRestart: Indicates that the Activity is being restarted. In general, onRestart is called when the current Activity changes from invisible to visible. When the user presses the Home button to switch to the desktop or opens a new Activity, the current Activity is paused. OnPause and onStop are executed, and the user returns to the Activity. That’s what happens. (onStart is still executed after onRestart. OnRestart cannot replace onStart.)

  • OnStart: Indicates that the Activity is being started and is about to start. At this point, the Activity is visible, but not yet in the foreground and can’t interact with the user. The Activity is already displayed, but we can’t see it yet.

  • OnResume: Indicates that the Activity is already visible and appears in the foreground to start the Activity. Notice how this compares to onStart. Both onStart and onResume indicate that the Activity is already visible, but onStart is in the background and onResume is in the foreground.

  • OnPause: Indicates that the Activity is being stopped, normally followed by onStop. In special cases, onResume is called if you quickly return to the current Activity. But this is such an extreme case that it’s hard for users to reproduce it. You can do things like store data, stop animations, and so on, but don’t take too long because this will affect the display of the new Activity. OnPause must be finished before the onResume of the new Activity can be executed.

  • OnStop: Indicates that the Activity is about to stop. You can do some heavy recycling, but not too long.

  • OnDestroy: Indicates that the Activity is about to be destroyed. This is the last callback in the Activity lifecycle, where we can do some recycling and finally release resources.

The specific implementation of these life cycles can be represented by the following diagram:

A special case

Is there a special case that causes the Activity lifecycle to be different from the normal case? Yes, but the main steps are the same as the normal case, with two more steps: OnSaveInstanceState and onRestoreInstanceState, these two steps are added when the Activity is not finished by us but is killed by the system due to insufficient memory or other exceptions.

OnSaveInstanceState is usually called before onStop. It stores Activity state and data.

OnRestoreInstanceState is usually called after onStart and restores data stored when the Activity exits.

In general, onSaveInstanceState and onRestoreInstanceState are not called by an Activity.

practice

Practice is the only criterion for testing truth, and knowledge gained by doing it yourself will be more solid, so let’s take a look at the life cycle of two activities.

We create a new Activity whose interface content is HelloWorld automatically generated by Androidstudio, and add a click event for HelloWorld to exit the interface. For easy observation, we output logs with error level Logcat.

class MainActivity : AppCompatActivity() {

    val TAG = this.javaClass.name

    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.e(TAG, "onCreate")}override fun onStart(a) {
        super.onStart()
        Log.e(TAG, "onStart")}override fun onResume(a) {
        super.onResume()
        Log.e(TAG, "onResume")}override fun onRestart(a) {
        super.onRestart()
        Log.e(TAG, "onRestart")}override fun onPause(a) {
        super.onPause()
        Log.e(TAG, "onPause")}override fun onStop(a) {
        super.onStop()
        Log.e(TAG, "onStop")}override fun onDestroy(a) {
        super.onDestroy()
        Log.e(TAG, "onDestroy")}}Copy the code

When we run the program and click on HelloWorld, we see the following message in the log.

2021- 08 -13 15:12:07.131 12133-12133/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onCreate
2021- 08 -13 15:12:07.138 12133-12133/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onStart
2021- 08 -13 15:12:07.140 12133-12133/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onResume
2021- 08 -13 15:12:23.509 12133-12133/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onPause
2021- 08 -13 15:12:23.871 12133-12133/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onStop
2021- 08 -13 15:12:23.875 12133-12133/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onDestroy
Copy the code

This is the same as what we described above. Let’s simulate the exception case again by adding the two methods of the exception case to the code and adding an EditText (the EditText must have an ID, otherwise the contents will not be saved).

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.e(TAG, "onSaveInstanceState")}// Note that there are two methods in the activity with the same name, but this one takes only one argument
    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.e(TAG, "onRestoreInstanceState")}Copy the code

We turn on the screen rotation and go to the screen rotation, enter the text in EditText and rotate the screen, and you can see the following information in the log.

2021- 08 -13 15:31:32.962 15658-15658/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onPause
2021- 08 -13 15:31:32.964 15658-15658/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onStop
2021- 08 -13 15:31:32.967 15658-15658/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onSaveInstanceState
2021- 08 -13 15:31:32.969 15658-15658/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onDestro
2021- 08 -13 15:31:33.054 15658-15658/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onCreate
2021- 08 -13 15:31:33.058 15658-15658/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onStart
2021- 08 -13 15:31:33.063 15658-15658/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onRestoreInstanceState
2021- 08 -13 15:31:33.065 15658-15658/com.cpw.learningapplication E/com.cpw.learningapplication.MainActivity: onResume
Copy the code

That’s all about the Activity lifecycle. If you have any questions, please comment!

Refer to The Art of Exploring Android Development by Ren Yugang