As mentioned in the introduction to lifecycle management, when an activity is paused or terminated, its status is retained. Indeed, because the activity is paused or terminated, it is still stored in the memory-all information about its members and current status is still stored, in this way, any changes to the user mode in the activity will be retained so that it can return to the foreground, and those changes will still exist.
However, when the system destroys an activity to restore the memory, the activity object is destroyed. Therefore, the system cannot simply use its complete State to restore it. On the contrary, if the user navigation returns the activity, the system must recreate it. However, the user cannot perceive the reconstruction of the activity destroyed by the system. Therefore, the user may expect the activity to be in the same status as before. In this case, you can use the onsaveinstancestate () callback method to save activity status information to ensure that important activity information is saved.
The system calls the onsaveinstancestate () method before the activity is destroyed. The system passes a bundle object to this method. In this object, you can use names/value pairs such as putstring () and putint () to save activity status information. Then, if the system kills your application process and the user returns to the activity, the system will recreate the activity and give oncreate () and onrestoreinstancestate () method to pass this bundle object. Using these methods, you can extract the status information you saved from the bundle object and restore the activity status. If the status information is not restored, a null bundle object will be passed to you (this bundle object is null when the activity is first created ).
Pic1
Figure 2
There are two methods to return an activity with the complete status to the user. When an activity is destroyed, you can use the previously mentioned method of restoring the storage state to recreate the activity. When the activity is terminated, the activity can be restored and maintained.
Note: before the activity is destroyed, the onsaveinstancestate () method cannot be called because the onsaveinstancestate () method does not need to be saved in some scenarios. For example, when you exit the activity by using the "back-to-back" key, because the user explicitly does not need to save the status to close the activity ). If the system wants to call the onsaveinstancestate () method, it can be called before the onstop () method and the onpause () method.
However, even if you haven't done anything and haven't implemented the onsaveinstancestate () method, you can use the default onsaveinstancestate () method of the activity class to restore some activity states. In particular, the default implementation of each view in the layout calls the onsaveinstancestate () method of the response, which allows each view to provide its own information that should be saved. Almost every widget in the android framework implements this method properly. For example, when a user is rebuilt, any changes to the visible attributes of the UI are automatically saved and restored. For example, the edittext control saves any text entered by the user and whether the checkbox control is saved by check. All you need to do is provide a unique ID (using the Android: Id attribute) for each visible control to save the status. If the visual controls do not have a unique ID, the system will not save their status.
Although the default Implementation of onsaveinstancestate () saves useful information on the interface, you can override this method to save additional information, such, you may need to save the changed Member values during activity survival (the relevant values in the UI may be restored, but by default, members with these UI values will not be restored ).
Because the default implementation of the onsaveinstancestate () method is to help save the UI state. If this method is rewritten to save additional state information, you should call the onsaveinstancestate () method of the parent class before doing any work. Similarly, if you want to override the onrestoreinstancestate () method, you should first call the parent class implementation of this method so that the default implementation can restore the view State.
Note: Because onsaveinstancestate () is not guaranteed to be called, you should only use it to record activity state changes (the State of the user interface), rather than using it to save persistent data. Instead, when a user leaves the activity, you should use the onpause () method to save persistent data (for example, data that should be stored in the database ).
Test ApplicationProgramA good way to restore their status is to simply rotate the device and change the screen direction of the device. When the screen direction changes, the system will destroy the activity and recreate a new activity in order to select a valid application resource for the new screen configuration. For this reason alone, it is vital to completely restore the status of the activity when it is re-built, because the user often rotates the screen when using the application.