OnCreate, onStart, onResume, onPause, onStop, onDestroy, onRestart and four states (Active, Paused, Stopped, Dead) are easy to remember. However, switching between multiple pages, AlertDialog, activity pop-ups, lock screen, unscreen, vertical screen switching and life cycle changes are all a bit confusing. This article will be listed above several scenarios in the code under the verification, deepen the understanding, deepen the impression.

Page switching, pop-up caused by the life cycle change problem.

1. Start a new Activity

The 2021-08-03 16:03:02. 261, 29605-29605 / com. Example. Myfirstproject D/MainActivity - TEST: OnCreate 16:03:02. 2021-08-03, 431, 29605-29605 / com. Example. Myfirstproject D/MainActivity - TEST: OnStart 16:03:02. 2021-08-03, 435, 29605-29605 / com. Example. Myfirstproject D/MainActivity - TEST: onResume -----jump secondActivity--------Copy the code

The 2021-08-03 16:03:26. 400, 29605-29605 / com. Example, myfirstproject D/MainActivity - TEST: onPause

The 2021-08-03 16:03:26. 432, 29605-29605 / com. Example. Myfirstproject D/SecondActivity - TEST: OnCreate 16:03:26. 2021-08-03, 483, 29605-29605 / com. Example. Myfirstproject D/SecondActivity - TEST: OnStart 16:03:26. 2021-08-03, 485, 29605-29605 / com. Example. Myfirstproject D/SecondActivity - TEST: onResumeCopy the code

The 2021-08-03 16:03:26. 782, 29605-29605 / com. Example, myfirstproject D/MainActivity - TEST: onStop

The 2021-08-03 16:03:26. 784, 29605-29605 / com. Example. Myfirstproject D/MainActivity - TEST: onSaveInstanceStateBundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@eb731 72, 2131230768=androidx.appcompat.widget.Toolbar$SavedState@53cf9c3, 2131230770=android.view.AbsSavedState$1@eb73172, 2131230776=android.view.AbsSavedState$1@eb73172, 2131230843=android.view.AbsSavedState$1@eb73172, 2131231130=com.google.android.material.button.MaterialButton$SavedState@ee1f240, 2131231131=android.view.AbsSavedState$1@eb73172, 2131231132=com.google.android.material.button.MaterialButton$SavedState@b83d579, 2131231133=com.google.android.material.button.MaterialButton$SavedState@6c2f2be, 2131231134=android.view.AbsSavedState$1@eb73172}}], androidx.lifecycle.BundlableSavedStateRegistry.key=Bundle[{}], android:lastAutofillId=1073741823, Android: fragments could = android. App. FragmentManagerState @ 3 eb6f1f}] -- -- -- -- -- lock screen -- -- -- -- -- -- -- the 2021-08-03 16:04:26. 542 29605-29605/com.example.myfirstproject D/SecondActivity-TEST: OnPause 16:04:26. 2021-08-03, 616, 29605-29605 / com. Example. Myfirstproject D/SecondActivity - TEST: OnStop 16:04:26. 2021-08-03, 628, 29605-29605 / com. Example. Myfirstproject D/SecondActivity - TEST: onSaveInstanceStateBundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@eb731 72, 2131230768=androidx.appcompat.widget.Toolbar$SavedState@85bab1, 2131230770=android.view.AbsSavedState$1@eb73172, 2131230776=android.view.AbsSavedState$1@eb73172, 2131230843=android.view.AbsSavedState$1@eb73172}}], androidx.lifecycle.BundlableSavedStateRegistry.key=Bundle[{}], android:lastAutofillId=1073741823, Android: fragments could = android. App. FragmentManagerState @ db89b96}] -- -- -- -- -- - XieBing -- -- -- -- -- the 2021-08-03 16:05:55. 354 29605-29605/com.example.myfirstproject D/SecondActivity-TEST: OnRestart 16:05:55. 2021-08-03, 355, 29605-29605 / com. Example. Myfirstproject D/SecondActivity - TEST: OnStart 16:05:55. 2021-08-03, 356, 29605-29605 / com. Example. Myfirstproject D/SecondActivity - TEST: OnResume -- -- -- -- -- - return to the previous page mainActivity -- -- -- -- -- - physical back 16:06:35. 2021-08-03, 619, 29605-29605 / com. Example. Myfirstproject D/SecondActivity - TEST: onPause 16:06:35. 2021-08-03, 688, 29605-29605 / com. Example. Myfirstproject D/MainActivity - TEST: OnRestart 16:06:35. 2021-08-03, 698, 29605-29605 / com. Example. Myfirstproject D/MainActivity - TEST: OnStart 16:06:35. 2021-08-03, 703, 29605-29605 / com. Example. Myfirstproject D/MainActivity - TEST: OnResume 16:06:36. 2021-08-03, 000, 29605-29605 / com. Example. Myfirstproject D/SecondActivity - TEST: OnStop 16:06:36. 2021-08-03, 001, 29605-29605 / com. Example. Myfirstproject D/SecondActivity - TEST: onDestroyCopy the code

These are the standard lifecycle changes. Note when the first Activity lifecycle onPause and onStop are executed. OnStop is executed after the second Activity onResume.

2. Open the AlertDialog window

The current Activity life cycle does not change.

An AlertDialog is only added to the current Activity layout by AddView, so it’s still in the current Activity.

3. Start an Activity whose Theme is a fully occluded Dialog Theme

The 2021-08-03 16:18:07. 207, 31534-31534 / com. Example. Myfirstproject D/MainActivity - TEST: OnCreate 16:18:07. 2021-08-03, 381, 31534-31534 / com. Example. Myfirstproject D/MainActivity - TEST: OnStart 16:18:07. 2021-08-03, 385, 31534-31534 / com. Example. Myfirstproject D/MainActivity - TEST: OnResume -- -- -- -- -- -- -- -- -- -- -- -- open dialogActivity -- -- -- -- -- -- -- -- the 2021-08-03 16:18:29. 276, 31534-31534 / com. Example. Myfirstproject D/MainActivity - TEST: onPause 16:18:29. 2021-08-03, 305, 31534-31534 / com. Example. Myfirstproject D/DialogActivity - TEST: OnCreate 16:18:29. 2021-08-03, 347, 31534-31534 / com. Example. Myfirstproject D/DialogActivity - TEST: OnStart 16:18:29. 2021-08-03, 349, 31534-31534 / com. Example. Myfirstproject D/DialogActivity - TEST: OnResume 16:18:29. 2021-08-03, 668, 31534-31534 / com. Example. Myfirstproject D/MainActivity - TEST: OnStop 16:18:29. 2021-08-03, 671, 31534-31534 / com. Example. Myfirstproject D/MainActivity - TEST: onSaveInstanceStateBundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@813f2 6c, 2131230768=androidx.appcompat.widget.Toolbar$SavedState@fe35035, 2131230770=android.view.AbsSavedState$1@813f26c, 2131230776=android.view.AbsSavedState$1@813f26c, 2131230843=android.view.AbsSavedState$1@813f26c, 2131231130=com.google.android.material.button.MaterialButton$SavedState@4377cca, 2131231131=android.view.AbsSavedState$1@813f26c, 2131231132=com.google.android.material.button.MaterialButton$SavedState@1e1de3b, 2131231133=com.google.android.material.button.MaterialButton$SavedState@b83c958, 2131231134=android.view.AbsSavedState$1@813f26c}}], androidx.lifecycle.BundlableSavedStateRegistry.key=Bundle[{}], android:lastAutofillId=1073741823, Android: fragments could = android. App. 85 bab1 FragmentManagerState @}] -- -- -- -- -- -- -- -- -- lock screen -- -- -- -- -- -- -- -- -- - the 2021-08-03 16:19:16. 524 31534-31534/com.example.myfirstproject D/DialogActivity-TEST: OnPause 16:19:16. 2021-08-03, 586, 31534-31534 / com. Example. Myfirstproject D/DialogActivity - TEST: OnStop 16:19:16. 2021-08-03, 588, 31534-31534 / com. Example. Myfirstproject D/DialogActivity - TEST: onSaveInstanceStateBundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@813f2 6c, 2131230768=androidx.appcompat.widget.Toolbar$SavedState@9bfd9b3, 2131230770=android.view.AbsSavedState$1@813f26c, 2131230776=android.view.AbsSavedState$1@813f26c, 2131230843=android.view.AbsSavedState$1@813f26c}}], androidx.lifecycle.BundlableSavedStateRegistry.key=Bundle[{}], android:lastAutofillId=1073741823, Android: fragments could = android. App. FragmentManagerState @ e766b70}] -- -- -- -- -- -- -- -- XieBing -- -- -- -- -- the 2021-08-03 16:19:44. 208 31534-31534/com.example.myfirstproject D/DialogActivity-TEST: OnRestart 16:19:44. 2021-08-03, 209, 31534-31534 / com. Example. Myfirstproject D/DialogActivity - TEST: OnStart 16:19:44. 2021-08-03, 210, 31534-31534 / com. Example. Myfirstproject D/DialogActivity - TEST: OnResume -- -- -- -- -- -- -- return to the previous page mainActivity -- -- -- -- -- - physical back 16:20:54. 2021-08-03, 847, 31534-31534 / com. Example. Myfirstproject D/DialogActivity - TEST: onPause 16:20:54. 2021-08-03, 885, 31534-31534 / com. Example. Myfirstproject D/MainActivity - TEST: OnRestart 16:20:54. 2021-08-03, 888, 31534-31534 / com. Example. Myfirstproject D/MainActivity - TEST: OnStart 16:20:54. 2021-08-03, 895, 31534-31534 / com. Example. Myfirstproject D/MainActivity - TEST: OnResume 16:20:55. 2021-08-03, 218, 31534-31534 / com. Example. Myfirstproject D/DialogActivity - TEST: OnStop 16:20:55. 2021-08-03, 220, 31534-31534 / com. Example. Myfirstproject D/DialogActivity - TEST: onDestroyCopy the code

Compare this to first (start an Activity). The life cycle changes exactly the same.

4. Start an Activity whose Theme is a half-masked Dialog Theme

The 2021-08-03 16:11:05. 582, 30559-30559 / com. Example. Myfirstproject D/MainActivity - TEST: OnCreate 16:11:05. 2021-08-03, 750, 30559-30559 / com. Example. Myfirstproject D/MainActivity - TEST: OnStart 16:11:05. 2021-08-03, 754, 30559-30559 / com. Example. Myfirstproject D/MainActivity - TEST: OnResume ------------ Open dialogActivity--------Copy the code

The 2021-08-03 16:11:24. 890, 30559-30559 / com. Example, myfirstproject D/MainActivity - TEST: onPause

The 2021-08-03 16:11:24. 928, 30559-30559 / com. Example. Myfirstproject D/DialogActivity - TEST: OnCreate 16:11:24. 2021-08-03, 965, 30559-30559 / com. Example. Myfirstproject D/DialogActivity - TEST: OnStart 16:11:24. 2021-08-03, 966, 30559-30559 / com. Example. Myfirstproject D/DialogActivity - TEST: OnResume -- -- -- -- -- -- -- -- -- lock screen -- -- -- -- -- -- -- -- -- - the 2021-08-03 16:12:25. 088, 30559-30559 / com. Example. Myfirstproject D/DialogActivity - TEST: OnPause 16:12:25. 2021-08-03, 204, 30559-30559 / com. Example. Myfirstproject D/DialogActivity - TEST: OnStop 16:12:25. 2021-08-03, 207, 30559-30559 / com. Example. Myfirstproject D/DialogActivity - TEST: onSaveInstanceStateBundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@7d662 27, 2131231103=android.view.AbsSavedState$1@7d66227}}], androidx.lifecycle.BundlableSavedStateRegistry.key=Bundle[{}], android:lastAutofillId=1073741823, Android: fragments could = android. App. FragmentManagerState @ 959 2021-08-03 16:12:25 dcd4}]. 212 30559-30559/com.example.myfirstproject D/MainActivity-TEST: OnStop 16:12:25. 2021-08-03, 216, 30559-30559 / com. Example. Myfirstproject D/MainActivity - TEST: onSaveInstanceStateBundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@7d662 27, 2131230768=androidx.appcompat.widget.Toolbar$SavedState@ee1f240, 2131230770=android.view.AbsSavedState$1@7d66227, 2131230776=android.view.AbsSavedState$1@7d66227, 2131230843=android.view.AbsSavedState$1@7d66227, 2131231130=com.google.android.material.button.MaterialButton$SavedState@b83d579, 2131231131=android.view.AbsSavedState$1@7d66227, 2131231132=com.google.android.material.button.MaterialButton$SavedState@6c2f2be, 2131231133=com.google.android.material.button.MaterialButton$SavedState@3eb6f1f, 2131231134=android.view.AbsSavedState$1@7d66227}}], androidx.lifecycle.BundlableSavedStateRegistry.key=Bundle[{}], android:lastAutofillId=1073741823, Android: fragments could = android. App. 813 f26c FragmentManagerState @}] -- -- -- -- -- -- -- -- XieBing -- -- -- -- -- the 2021-08-03 16:13:22. 628 30559-30559/com.example.myfirstproject D/MainActivity-TEST: OnRestart 16:13:22. 2021-08-03, 631, 30559-30559 / com. Example. Myfirstproject D/MainActivity - TEST: OnStart 16:13:22. 2021-08-03, 633, 30559-30559 / com. Example. Myfirstproject D/DialogActivity - TEST: OnRestart 16:13:22. 2021-08-03, 633, 30559-30559 / com. Example. Myfirstproject D/DialogActivity - TEST: OnStart 16:13:22. 2021-08-03, 635, 30559-30559 / com. Example. Myfirstproject D/DialogActivity - TEST: OnResume -- -- -- -- -- -- -- return to the previous page mainActivity -- -- -- -- -- - physical back 16:16:34. 2021-08-03, 631, 30559-30559 / com. Example. Myfirstproject D/DialogActivity-TEST: onPauseCopy the code

The 2021-08-03 16:16:34. 731, 30559-30559 / com. Example, myfirstproject D/MainActivity - TEST: onResume

The 2021-08-03 16:16:34. 790, 30559-30559 / com. Example. Myfirstproject D/DialogActivity - TEST: OnStop 16:16:34. 2021-08-03, 792, 30559-30559 / com. Example. Myfirstproject D/DialogActivity - TEST: onDestroyCopy the code

Compared to the first step (starting an Activity), the first Activity only performs onPause and does not execute onStop after the second Activity onResume. When the physical button returns to the first page, only onResume is executed, and only the onResume corresponding to the onPause lifecycle is executed.


To sum up:

The core of life cycle change is whether the old page is completely blocked by the new page. What affects is whether the old page life cycle onStop method is executed.

Full occlusion, onPause- “New page life cycle -” onStop.

Semi-occlusion: onPause- New page life cycle.


Ii. Horizontal and vertical screen switching life cycle changes:

We know have been set the android: configChanges = “orientation” | keyboardHidden | screenSize “can disable screen switch changes caused by life cycle, so as to solve the problem of page destroyed to create.

The next step is to change the android:configChanges property to see how the screen life cycle changes.

1. Property Settings for life cycle changes

android:configChanges=”” android:configChanges=”orientation” android:configChanges=”orientation|keyboardHidden” android:configChanges=”screenSize”

The 2021-08-03 17:08:00. 364, 5717-5717 / com. Example. Myfirstproject D/MainActivity - TEST: OnCreate 17:08:00. 2021-08-03, 532, 5717-5717 / com. Example. Myfirstproject D/MainActivity - TEST: OnStart 17:08:00. 2021-08-03, 535, 5717-5717 / com. Example. Myfirstproject D/MainActivity - TEST: Rotation onResume -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - the 2021-08-03 17:08:41. 253, 5717-5717 / com. Example. Myfirstproject D/MainActivity - TEST: OnPause 17:08:41. 2021-08-03, 259, 5717-5717 / com. Example. Myfirstproject D/MainActivity - TEST: OnStop 17:08:41. 2021-08-03, 263, 5717-5717 / com. Example. Myfirstproject D/MainActivity - TEST: onSaveInstanceStateBundle[{android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1@77b2f 2d, 2131230768=androidx.appcompat.widget.Toolbar$SavedState@1a0fc62, 2131230770=android.view.AbsSavedState$1@77b2f2d, 2131230776=android.view.AbsSavedState$1@77b2f2d, 2131230843=android.view.AbsSavedState$1@77b2f2d, 2131231130=com.google.android.material.button.MaterialButton$SavedState@43d81f3, 2131231131=android.view.AbsSavedState$1@77b2f2d, 2131231132=com.google.android.material.button.MaterialButton$SavedState@ba8eb0, 2131231133=com.google.android.material.button.MaterialButton$SavedState@3089129, 2131231134=android.view.AbsSavedState$1@77b2f2d}}], androidx.lifecycle.BundlableSavedStateRegistry.key=Bundle[{}], android:lastAutofillId=1073741823, Android: fragments could = android. App. FragmentManagerState @ 5967 cae}] 17:08:41 2021-08-03. 265 5717-5717/com.example.myfirstproject D/MainActivity-TEST: OnDestroy 17:08:41. 2021-08-03, 701, 5717-5717 / com. Example. Myfirstproject D/MainActivity - TEST: OnCreate 17:08:41. 2021-08-03, 793, 5717-5717 / com. Example. Myfirstproject D/MainActivity - TEST: OnStart 17:08:41. 2021-08-03, 794, 5717-5717 / com. Example. Myfirstproject D/MainActivity - TEST: OnRestoreInstanceState 17:08:41. 2021-08-03, 795, 5717-5717 / com. Example. Myfirstproject D/MainActivity - TEST: onResumeCopy the code

2. Property Settings whose life cycle does not change

//android:configChanges=”orientation|keyboardHidden|screenSize” android:configChanges=”orientation|screenSize”

The 2021-08-03 17:11:47. 546, 6771-6771 / com. Example. Myfirstproject D/MainActivity - TEST: onConfigurationChanged {1.0? mcc? mnc [zh_CN_#Hans,en_GB] ldltr sw360dp w640dp h336dp 480dpi nrml long land finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1920, 1080) mAppBounds=Rect(0, 0 - 1920, 1080) mWindowingMode=fullscreen mActivityType=standard} nonFullScreen=0 suim:1 s.24}Copy the code

Is also set the “orientation” | screenSize “and” orientation “| keyboardHidden | screenSize” will disable screen for life cycle change, That is really disabled somehow the panel switch is “orientation” | screenSize “attribute, with no melon keyboardHidden.

“Orientation” | screenSize “attribute will not make destroyed to create the page, but will expose onConfigurationChanged method calls to us.


To sum up:

Ban somehow the screen switch retrace life cycle using: android: configChanges = “orientation” | screenSize”

If somehow the screen to switch to retrace life cycle, it is necessary to save the data: onSaveInstanceStateBundle preservation, onRestoreInstanceState recovery.