Activity is one of the four components of Android, and this article summarizes the various approaches to the lifecycle. I didn’t post any code to print the log, so you can try it out for yourself, see if this summary is correct, and if not, just submit a comment.

A classic lifecycle flowchart


The end of the I

  • 1. Open a new activityA: oncreat—– onStart — onResume
  • 2, from activityA — activityB (full screen) : activityA first onPause, then activityA oncreat—–onstart– onResume, activityA onStop
  • 3, from activityA — activityB (non-full screen) : activityA does onPause, then activityA does oncreat—–onstart– onResume, activityA does not do onStop
  • 4, activityB (full screen) return to activityA: OnRestart -> onStart ->onResume, activityB is executing onstop — onDestory;
  • 5, activityB (not full screen) return to activityA: activityB: onPause, activityA: onResume, activityB: onstop — onDestory;
  • 6, activityB returns to activityA: If activityA has been destroyed, activityA will be created again, run oncreat—–onstart– onResume, the process of activityB remains the same
  • 7, activityA press the home button back to the background: the same 2 process, from home back to activityA, the same 4 process;

    Some special methods

    OnWindowFocusChanged: Called when the Activity window gains or loses focus and after onResume when the Activity is created, when the Activity is overwritten or goes into the background, or when the current Activity exits, It is called after onPause (in this method you can get the width and height properties of the view after the view has been drawn)

    OnSaveInstanceState: (1) This method is called when an Activity is overwritten or left in the background and is killed by insufficient system resources; (2) This method is called when the user changes the direction of the screen; (3) This method is called when the current Activity jumps to another Activity or presses the Home button to return to the Home screen and recures into the background. In the first case, we cannot guarantee when it will happen, and the system will schedule according to the intensity of resources. Second, when the screen flips, the system destroys the current Activity and then recreates a new one. When we call this method, we can save some temporary data. In the third case, the system calls this method to save the state of each View component in the current window. OnSaveInstanceState is called in the order before onStop. (before android3.0: call before onPause, after 3.0, call after onPause)

    OnRestoreInstanceState: Some people say that this method is a pair of onSaveInstanceState, but it is not. (1) This method is called when the user returns to the Activity after the Activity has been overwritten or gone into the background, and the system has insufficient resources to kill it. (2) This method will be called during the reconstruction process when the user changes the screen orientation. We can override this method so that some temporary data can be recovered. OnRestoreInstanceState is called in the order after onStart. (onRestoreInstanceState is not called, but onSaveInstanceState is. Here’s the difference.)

    Final summary

    When an Activity is rebuilt after it has been destroyed by the system, the sequence of function calls to save and restore data is: OnSaveInstanceState (save data)–>onCreate(restore data Allstate)–>onRestoryInstanceState(restore data HierarchyState)

    To unswitch the screen method and rebuild the activity, you can configure the configChanges property: Android: configChanges = “keyboardHidden | orientation | screenSize (when support is greater than the minimum SDK version of android4.0 need to set this property)”