android horizontal portrait switch Activity life cycle

  • 2020-05-09 19:14:52
  • OfStack

1. Create a new Activity and print out each life cycle

2. Run Activity and get the following information

onCreate-- >
onStart-- >
onResume-- >

3. Press crtl+f12 to switch to landscape

onSaveInstanceState-- >
onPause-- >
onStop-- >
onDestroy-- >
onCreate-- >
onStart-- >
onRestoreInstanceState-- >
onResume-- >

4. When pressing crtl+f12 to switch to portrait, I found that the same log was printed twice

onSaveInstanceState-- >
onPause-- >
onStop-- >
onDestroy-- >
onCreate-- >
onStart-- >
onRestoreInstanceState-- >
onResume-- >
onSaveInstanceState-- >
onPause-- >
onStop-- >
onDestroy-- >
onCreate-- >
onStart-- >
onRestoreInstanceState-- >
onResume-- >

5. Modify AndroidManifest.xml and add the Activity

android:configChanges="orientation", follow step 3
onSaveInstanceState-- >
onPause-- >
onStop-- >
onDestroy-- >
onCreate-- >
onStart-- >
onRestoreInstanceState-- >
onResume-- >

6. Execute step 4 again and find that the same information will not be printed again, but 1 more line onConfigChanged is printed

onSaveInstanceState-- >
onPause-- >
onStop-- >
onDestroy-- >
onCreate-- >
onStart-- >
onRestoreInstanceState-- >
onResume-- >
onConfigurationChanged-- >

7. Change android in step 5 :configChanges="orientation"
android:configChanges="orientation|keyboardHidden", follow step 3 and print only
onConfigChanged
onConfigurationChanged-- >

8. Execute step 4

onConfigurationChanged-- >
onConfigurationChanged-- >

Conclusion:

1. When android of Activity is not set :configChanges, the screen cut will call each life cycle again.
We do it once when we cut landscape, twice when we cut portrait
2. When setting android of Activity :configChanges="orientation", the screen cut will be adjusted again
With each life cycle, the horizontal and portrait will be cut only once
3. When setting android of Activity :configChanges="orientation|keyboardHidden",
The screen cut does not re-invoke the lifecycle, only the onConfigurationChanged method is executed

Summarize 1 the entire Activity lifecycle
To add 1 point, the current Activity generation event ejects Toast and AlertDialog when Activity lives
The period doesn't change
When Activity runs, press HOME (as if it were completely covered) : onSaveInstanceState -- >
onPause -- > onStop onRestart -- > onStart--- > onResume
Activity is not completely covered and just loses focus: onPause-- > onResume

Related articles: