1. Detailed explanation of life cycle processes & methods
1.1 Please see the figure below for details
1.2 Precautions
a. Life cycle approach = appear in pairs (pairing)
onCreate()
&onDestory()
onStart()
&onStop()
onResume()
&onPause()
b. onStart() & onStop(), onResume() & onPause() Except for the callback moment, there is no difference in actual use
onStart()
&onStop()
: From theActivity
callback is fully visible angleonResume()
&onPause()
: FromActivity
a callback whether in the foreground (UI topmost) point of view;- In addition to the above differences, there is no difference in actual use
c. The current Activity is A. After the user opens ActivityB, which method of A's onPause() and B's onResume() will be executed first?
Answer: First A onPause()
, then BonResume()
Activity
Startup process:ActivityManagerService
(AMS)Activity
synchronizes the management of the state in the stack & stipulates: Before a newActivity
start, the Activity at the top of the stack must firstonPause()
be able to start a new oneActivity
(executeonResume()
)
Note: In order for the new
Activity
switch to the front as soon as possible, inonPause()
as much as possible not to do time-consuming / heavyweight operation
2. The life cycle calling method of common scenarios
3. Comparison with Fragment life cycle
Fragment
,Activity
The life cycle is very similar- The specific comparison is as follows:
1. When the activity's android:configChanges is not set, each life cycle will be re-invoked when the screen is cut. It will be executed once when the horizontal screen is cut and twice when the vertical screen is cut.
2. When the activity's android:configChanges="orientation" is set, Each life cycle will be called again when the screen is cut, and it will only be executed once when the screen is cut horizontally or vertically.
3. When the activity's android:configChanges="orientation|keyboardHidden" is set, the screen cut will not re-call each life cycle, only onConfigurationChanged will be executed. method
PS:
The life cycle of the Activity will not change when the Toast and AlertDialog pop up when the current Activity generates events
Press the HOME button when the Activity is running (the same as being completely covered): onSaveInstanceState --> onPause --> onStop onRestart -->onStart--->onResume
Activity is not completely covered but just loses focus: onPause--->onResume