The Reclaim mechanism of the Android system destroys an activity without the user’s active action, and to avoid data loss caused by the system’s reclaim activity, Android provides onSaveInstanceState(Bundle outState) and onRestoreInstanceState(Bundle savedInstanceState) for saving and restoring data.

When onSaveInstanceState(Bundle outState) is called?

The answer is when the activity is likely to be reclaimed by the system, and that is before onStop(). Note that it is possible that onSaveInstanceState will not be called if it is already determined to be destroyed, such as when the user presses the return key or calls the Finish () method to destroy the activity. Alternatively, this method is called only if the activity is terminated abnormally.

To summarize, onSaveInstanceState(Bundle outState) is called when:

1. When the user presses the HOME button. 2. Choose to run another program from the most recent application. 3. Press the power button (turn off the screen display). 4. Start a new activity from the current activity. 5. When the screen direction is switched (it will be called whether portrait or landscape cutting portrait).Copy the code

In the first four cases, the current activity lifecycle is:

OnPause -> onSaveInstanceState -> onStop. (This is the result of my test, but according to Android Development Art Quest, the order of onPause and onSaveInstanceState is not necessarily the same.)Copy the code

When is onRestoreInstanceState called?

OnRestoreInstanceState (BundlesavedInstanceState) is only called if the activity is actually reclaimed by the system and recreated.

For example, in case 5, when the screen is switched, the activity lifecycle is as follows: onPause -> onSaveInstanceState -> onStop -> onDestroy -> onCreate -> onStart -> onRestoreInstanceState -> onResume Here onRestoreInstanceState is called because the original activity is actually reclaimed by the system when the screen is switched and a new activity is created. (By the way, I’d like to poke fun at some articles on the web that say that the activity lifecycle method is executed differently when portrait is cut from landscape, which is proved to be the same.)

When you press the HOME button to return to the desktop and immediately click the app icon to return to the original page, the activity lifecycle is as follows: OnPause -> onSaveInstanceState -> onStop -> onRestart -> onStart -> onResume So onRestoreInstanceState is not called.

If onRestoreInstanceState was called, the page must have been reclaimed, and onSaveInstanceState must have been called.

OnCreate () also has the Bundle parameter, which can be used to restore data.

Since onSaveInstanceState may not be called, the Bundle parameter in onCreate() may be empty. If onCreate() is used to restore data, make sure it is not empty.

The onRestoreInstanceState Bundle parameter must not be null because it will only be called the last time the activity was reclaimed.

And onRestoreInstanceState is called after onStart(). Sometimes we need to do some initialization in onCreate() and then restore the data. It’s convenient to use onRestoreInstanceState. Here’s what the official documentation says about onRestoreInstanceState:

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been doneor to allow subclasses to decide whether to use your default implementation. Notice what the last sentence of this instruction means? to allow subclasses to decide whether to use your default implementation. It says, using the onRestoreInstanceState method to recover data, you can decide whether to call the onRestoreInstanceState method of the parent class, Whether the call super onRestoreInstanceState (savedInstanceState); To restore data with onCreate(), you must call super.onCreate(savedInstanceState);Copy the code

@override public void onSaveInstanceState(Bundle savedInstanceState) {// We can convert static global variables to Json savedInstanceState.putBoolean("MyBoolean".true);
        savedInstanceState.putDouble("myDouble", 1.9);
        savedInstanceState.putInt("MyInt", 1);
        savedInstanceState.putString("MyString"."Welcome back to Android"); // etc. super.onSaveInstanceState(savedInstanceState); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Restore data from savedInstanceState, if no need to restore data savedInstanceState is nuLif(savedInstanceState ! = null) { boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
        double myDouble = savedInstanceState.getDouble("myDouble");
        int myInt = savedInstanceState.getInt("MyInt");
        String myString = savedInstanceState.getString("MyString"); Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); boolean myBoolean = savedInstanceState.getBoolean("MyBoolean");
        double myDouble = savedInstanceState.getDouble("myDouble");
        int myInt = savedInstanceState.getInt("MyInt");
        String myString = savedInstanceState.getString("MyString");
}
Copy the code