[Android] Analysis of Activity life cycle and horizontal and vertical screen switching


1. Detailed explanation of life cycle processes & methods

1.1 Please see the figure below for details

Schematic diagram

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 the Activitycallback is fully visible angle
  • onResume()& onPause(): From Activitya 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()

  • ActivityStartup process: ActivityManagerService(AMS) Activitysynchronizes the management of the state in the stack & stipulates: Before a new Activitystart, the Activity at the top of the stack must first onPause()be able to start a new one Activity(execute onResume())

Note: In order for the new Activityswitch to the front as soon as possible, in onPause()as much as possible not to do time-consuming / heavyweight operation


2. The life cycle calling method of common scenarios

Schematic diagram


3. Comparison with Fragment life cycle

  • Fragment, ActivityThe life cycle is very similar
  • The specific comparison is as follows:
    Comparison with Fragment life cycle

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

 


Guess you like

Origin blog.csdn.net/xfb1989/article/details/110189444