In depth analysis of android life cycle of I

  • 2020-05-07 20:25:02
  • OfStack

The Android system adds 1 hook to the Activity life cycle, and we can do 1 thing in the hook set aside by these systems.
Seven commonly used hooks are listed:
protected void onCreate(Bundle savedInstanceState)
protected void onStart()
protected void onResume()
protected void onPause()
protected void onStop()
protected void onRestart()
protected void onDestroy()

brief description :
onCreate(Bundle savedInstanceState) : called when activity is created. Set in the method http: / / www myexception. cn /, also would like to ask you to create the Activity in Bundle the required information.
onStart() : activity is called when it becomes visible to the user on the screen, that is, when it gets focus.
onResume() : called when activity starts interacting with the user (this method is always called whether an activity is started or restarted).
onPause() : called when activity is suspended or withdrawn from cpu and other resources. This method is used to save the active state.
onStop() : called when activity is stopped and becomes invisible to phase and subsequent lifecycle events, that is, when it loses focus.
onRestart() : called when activity is restarted. The activity is still on the stack, java.io.UnsupportedEncodingException instead of starting a new activity.
onDestroy() : when activity is completely removed from system memory, the method may be called because someone directly calls the finish() method or the system decides to stop the activity to free the resource.

horizontal portrait switch
1 switch to landscape
onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume

2 switches to portrait and destroys twice
onSaveInstanceState
onPause
onStop
onDestroyonCreate
onStart
onRestoreInstanceState
onResume
onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume

3 modify AndroidManifest.xml, add Activity to android:configChanges="orientation", cut landscape, and destroy only once
onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume

4 then cuts back to portrait and finds that it will not print the same information again, but it prints onConfigChanged one more line
onSaveInstanceState
onPause
onStop
onDestroy
onCreate
onStart
onRestoreInstanceState
onResume
onConfigurationChanged

5 change android:configChanges="orientation" to android:configChanges="orientation|keyboardHidden", cut landscape, just print onConfigChanged
onConfigurationChanged

6 goes back to portrait
onConfigurationChanged
onConfigurationChanged

summary :
1. When android:configChanges of Activity is not set, screen cutting will call each life cycle again, and it will be executed once when landscape is cut and twice when portrait is cut
2. When Activity's android:configChanges="orientation" is set, the screen cutting will still call each life cycle again, and the horizontal and portrait cutting will only be executed once
3. When android:configChanges="orientation|keyboardHidden" is set to Activity, screen cutting will not call each life cycle again, only onConfigurationChanged method will be executed

Related articles: