In several scenarios, your activity will be destroyed due to normal app behavior. For example, when you press the back button or call the finish () method to destroy yourself. The system will also destroy your activity. If it is currently stopped and not used for a long time or the foreground activity requires more resources, the system must disable the background process to recycle the memory.
When your activity is destroyed because the user presses the return key, the activity instance is permanently destroyed in the system, because this action indicates that the activity is no longer needed. However, if the system destroys an activity due to system constraints (rather than normal program behavior), although the actual activity instance is destroyed, the system remembers that it exists. If the user returns it, the system creates a new activity instance and uses a group of data that is stored when the instance is destroyed. The system uses the previous State to recover data called "instance state ", is a set of key-value pairs stored in the bundle object.
Note:When a user rotates the screen, your activity will be destroyed. When the screen changes direction, the system will destroy and re-create the foreground activity, because the screen configuration has changed, your activity may need to load alternative resources (such as layout ).
By default, the system uses the bundle instance to save the information of each view object in your activity layout file (for example, the text value enters the edittext object ). Therefore, if your active instance is destroyed and re-created, the layout status will be automatically restored. However, your activity may have more status information you want to restore, such as member variables that track the progress of your activity.
Note:To restore the Android system to the comments of your activity,Each view must have a unique ID, which is declared through the Android: Id attribute.
To save extra activity status data, you must override the onsaveinstancestate () callback method, the system calls this method to save events that are unexpectedly destroyed when the user leaves your activity and uses its bundle object. If the system must recreate the active instance, it will pass the same bundle object to the onrestoreinstancestate () and oncreate () methods.
Illustration: as the system starts to stop your activity, it calls onsaveinstancestate () (1). You can specify additional status data and save them to prevent the active instance from being created again. If the activity is destroyed, the same instance must be re-created. The system calls the oncreate () method (1), (2) and onrestoreinstancestate () method (3) and pass in the previously saved object for implementation.
Save the status of your activity
When your activity starts to stop, the system calls the onsaveinstancestate () method and places a State information in a key-value pair. By default, this method implements a view level that stores the information activity status, such as the position of the text or scroll List View in an edittext widget.
To save additional information about your activity, you must implement the onsaveinstancestate () method and add the key-value pair to the bundle object. For example:
Static FinalString state_score = "playerscore ";
Static FinalString state_level = "playerlevel ";
...
@ Override
Public VoidOnsaveinstancestate (bundle savedinstancestate ){
// Save the user's current game state
Savedinstancestate. putint (state_score, mcurrentscore );
Savedinstancestate. putint (state_level, mcurrentlevel );
// Always call the superclass so it can save the view hierarchy state
Super. Onsaveinstancestate (savedinstancestate );
}
Restore activity status
You can choose to implement the onrestoreinstancestate () method instead of the oncreate () method to restore the state. The system will call the onstart () method. The system will call the onrestoreinstancestate () method only when saved information needs to be restored. Therefore, you do not need to check whether the bundle is null.
Public VoidOnrestoreinstancestate (bundle savedinstancestate ){
// Always call the superclass so it can restore the view hierarchy
Super. Onrestoreinstancestate (savedinstancestate );
// Restore state members from saved instance
Mcurrentscore = savedinstancestate. getint (state_score );
Mcurrentlevel = savedinstancestate. getint (state_level );
}
Note:Always call the onrestoreinstancestate () method of the parent class to restore the view level by default.
Recreating an activity