Cause Analysis:
My problem is that the previous switch to the test screen was set below in the manifest file, causing the screen to switch without calling onrestoreinstancestate and not executing the Ondestory method.
Android:configchanges= "Keyboard|screensize|orientation"
<activity android:name= ". Mainactivity "android:configchanges=" keyboard|screensize|orientation ">
<intent-filter>
< Action android:name= "Android.intent.action.MAIN"/>
<category android:name= " Android.intent.category.LAUNCHER "/>
</intent-filter>
</activity>
According to the official website description: "When your activity is recreated after the previous destruction, you can restore the saved state from the Bundle that the system passes to the activity." Both the OnCreate () and Onrestoreinstancestate () callback methods receive the same Bundle that contains the instance state information. ”
Because the Configchanges parameter set above causes the Ondestory method not to execute, it is not destroyed, so there is no such recovery.
Workaround:
You can temporarily remove this setting to see that the activity will also be in the screen switch when the call ondestory, while the screen switch when there is a callback onrestoreinstancestate,
Reference: HTTPS://DEVELOPER.ANDROID.GOOGLE.CN/TRAINING/BASICS/ACTIVITY-LIFECYCLE/RECREATING.HTML?HL=ZH-CN
Develop Training re-create activity This course will show you how to save the activity state to resume activity state You should also read support for different screen processing run time-varying More Activity
In some cases, your activity will be destroyed by normal application behavior, such as when the user presses the Back button or your activity by calling finish () to indicate their destruction. The system may also destroy activity if the activity is currently stopped or is not in use for a long time, or if the foreground activity requires more resources so that the system must shut down the background process to recover memory.
When your activity is destroyed as a result of a user's return or activity being done on its own, the system's activity instance concept will disappear permanently, as the behavior indicates that activity is no longer required. However, if the activity is destroyed by the system due to system limitations (rather than normal application behavior), the system remembers its existence, even though the actual instance of the activity is no longer present, so that if the user navigates back to the instance, the system creates a set of saved data that describes the state when the activity was destroyed A new instance of Activity. The saved data that the system uses to restore the previous state is referred to as the "instance state" and is a collection of key-value pairs stored in the Bundle object.
Note : Each time the user rotates the screen, your Activity will be destroyed and recreated. When the screen orientation changes, the foreground Activity is destroyed and recreated because the screen configuration has changed and your Activity may need to load alternate resources (such as layouts).
By default, the system uses the Bundle instance state to hold information about each View object in your Activity layout (for example, text values entered into the EditText object). This way, if your Activity instance is destroyed and recreated, the layout state reverts to its previous state, and you do not need code. However, your activity may have more state information that you want to recover, such as member variables that track the user's progress in Activity.
Note : For the Android system to restore the state of the view in Activity, each view must have a unique ID provided by the Android:id property.
To save additional data about the Activity state, you must override the Onsaveinstancestate () callback method. This method is called when the user wants to leave the activity and pass the Bundle object that will be saved when the activity is accidentally destroyed. If the system must recreate the Activity instance later, it will pass the same Bundle object to the Onrestoreinstancestate () and OnCreate () methods at the same time.
When the system starts to stop your activity, it calls Onsaveinstancestate () (1), so you can specify additional state data that you want to save when the activity instance must be recreated. If the Activity is destroyed and the same instance must be recreated, the system will pass the state data defined in (1) to the OnCreate () method (2) and the Onrestoreinstancestate () method (3).
Save Activity Status
When your activity starts to stop, the system calls Onsaveinstancestate () so that your activity can save state information with a set of key-value pairs. The default implementation of this method holds state information about the Activity view hierarchy, such as the text in the EditText widget or the scroll position of the ListView.
To save more state information for an Activity, you must implement Onsaveinstancestate () and add key-value pairs to the Bundle object. For example:
Static final String State_score = "Playerscore";
Static final String State_level = "PlayerLevel";
...
@Override public
void Onsaveinstancestate (Bundle savedinstancestate) {
//Save The user's current game stateSavedinstancestate.putint (State_score, mcurrentscore);
Savedinstancestate.putint (State_level, mcurrentlevel);
The superclass so it can save the view hierarchy State
super.onsaveinstancestate (savedinstancestate );
}
Note : The superclass Implementation of onsaveinstancestate () is always called so that the default implementation can save the state of the view hierarchy. Resume Activity Status
When your activity is recreated after it was previously destroyed, you can restore the saved state from the Bundle that the system passes to the activity. Both the OnCreate () and Onrestoreinstancestate () callback methods receive the same Bundle that contains the instance state information.
Because the OnCreate () method is called regardless of whether the system is creating a new instance of the Activity or recreating the previous instance, you must check that the state Bundle is null before attempting to read it. If NULL, the system creates a new instance of the Activity instead of recovering the previous instance that was destroyed.
For example, here shows how you can recover some state data in OnCreate ():
@Override
protected void onCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);//Always Call the superclass first
//Check Whether we ' re recreating a previously destroyed instance
if (Savedinsta Ncestate = null) {
//Restore value of saved state
Mcurrentscore = Savedinstancestat E.getint (State_score);
Mcurrentlevel = Savedinstancestate.getint (state_level);
} else {
//Probably Initialize members with default values for a new instance
}
...
}
You can choose to implement Onrestoreinstancestate () that the system calls after the OnStart () method, rather than recovering the state during onCreate (). The system calls Onrestoreinstancestate () only when there is a saved state to be restored, so you do not have to check if the Bundle is null:
public void Onrestoreinstancestate (Bundle savedinstancestate) {
//All call the superclass so it can restore th E View Hierarchy
super.onrestoreinstancestate (savedinstancestate);
Restore State members from saved instance
Mcurrentscore = Savedinstancestate.getint (state_score);
Mcurrentlevel = Savedinstancestate.getint (State_level);
}
Note : The superclass Implementation of onrestoreinstancestate () is always called so that the default implementation can restore the state of the view hierarchy.
To learn more about recreating activity as a result of a run-time restart event, such as screen rotation, read processing run-time changes.