Follow-up will continue to update, it is recommended to collect attention

When opening window B from window A, which window B’s onResume or A’s onPause method is executed first?

What is this question about

Have you tried to test the Activity lifecycle function yourself? Simply using the onResume function will not answer this question.

Subdivided down the knowledge point

  1. A opens B, how does A’s own life cycle execute, how does B’s life cycle execute
  2. From B to A, what is the order of execution

How to answer

1.启动 A onCreate – onStart – onResume

2. In A, start ActivityA onPause ActivityB onCreate ActivityB onStart ActivityB onResume ActivityA onStop

3. Return ActivityB to A (press the physical hardware return key). ActivityB onPause ActivityA onRestart ActivityA onStart ActivityA onResume ActivityB onStop ActivityB onDestroy

4. Return ActivityA onPause ActivityA onStop ActivityA onDestroy

These are the results that can be obtained directly from the log after doing the experiment. After summary and analysis, it is concluded that:

From A to B, A pause, B must walk the onCreate, onStart, onResume to interactive state, A would hand over control of the onStop. This prevents A from disappearing too quickly and causing the user to see that B is not ready. If B goes back to A, B should go onRestart-onStart-onResume before B should go onStop.

Figuratively speaking, the a-B switch is equivalent to the stage play scene switch. Before the curtain falls, the next scene must be completely ready, not A semi-finished state.


How to save the state of an Activity (state is data)

What is this question about

The state of an Activity is usually saved automatically by the system. It is saved only when additional data needs to be saved, or in special circumstances.

Subdivided down the knowledge point

  1. In what scenarios does the Activity state not need to be manually saved
  2. In what scenarios must MANUAL saving be performed
  3. Override which method to save data

How to answer

In general, activity instances that have been called onPause() and onStop() remain in memory. All information and state data about the activity do not disappear, and all changes are retained when the activity returns to the foreground. However, when the system runs out of memory, the activity after calling onPause() and onStop() may be destroyed by the system, and no instance objects of the activity will be stored in memory. If the activity comes back to the foreground later, the changes will disappear. To avoid this, we can override the onSaveInstanceState() method. The onSaveInstanceState() method takes a Bundle object into which the developer can store state data, thereby destroying the activity by the system. When the user restarts the activity and calls its onCreate() method, the above Bundle object is passed to the onCreate() method as an argument, and the developer can retrieve the saved data from the Bundle object. This data is then used to restore the activity to its state before it was destroyed. It is important to note that the onSaveInstanceState() method is not always called because some scenarios do not require state data to be saved. For example, if the user presses the BACK key to exit the activity and the user clearly wants to close the activity, there is no need to save the data for the next recovery, that is, the onSaveInstanceState() method is not called. If the onSaveInstanceState() method is called, the call will occur before the onPause() or onStop() methods.


The Activity life cycle when switching between horizontal and vertical screens

What is this question about

In addition to the normal call order of life cycle functions, the system will also change the call order of life cycle functions according to the Settings of APP.

Subdivided down the knowledge point

  1. How to configure the vertical screen switch Settings

How to answer

The life cycle at this point is related to the configuration in the manifest file.

1. Android :configChanges without setting the Activity, the screen cut recalls each lifecycle. By default, the current Activity is destroyed and then reloaded

2. Set up the Activity of the android: configChanges = “orientation” | keyboardHidden | screenSize “, each life cycle function switch will not call again, Only the onConfigurationChanged method is executed.