So far, we have learned that when the screen direction is changed, the activity will be destroyed and then rebuilt. Remember that when an activity is rebuilt, its current information may disappear. When an activity is killed, one or two of the following methods will be called:
OnPause ()-This method is always called when an activity is killed or transferred to the background.
OnSaveInstanceState () -- this method is also used when an activity is killed or transferred to the background, just like the onPause () method. However, when an activity is cleared from the call stack (for example, when the return key is pressed), this method will not be called (Note: The onPause () method will be called ), because there is no need to save its status.
In short, to save the activity status, you always need to implement the onPause () method, and then use your own method to save the status information, such as using a database, external or internal storage.
If you want to simply save the status information, load the information when the activity is re-created. A simple method is to implement the onSaveInstanceState () method, because it provides a Bundle object as a parameter, so that you can use this Bundle object to save the activity information. The following code shows how to use the onSaveInstanceState () method:
[Java]
@ Override
Public void onSaveInstanceState (Bundle outState ){
// --- Save whatever you need to persist ---
OutState. putString ("ID", "1234567890 ");
Super. onSaveInstanceState (outState );
}
When an activity is rebuilt, The onCreate () method will be called first, and then the onRestoreInstanceState () method, that is, this method can retrieve the information just saved through the onSaveInstanceState () method:
[Java]
@ Override
Public void onRestoreInstanceState (Bundle savedInstanceState ){
Super. onRestoreInstanceState (savedInstanceState );
// --- Retrieve the information persisted earlier ---
String ID = savedInstanceState. getString ("ID ");
}
Although the onSavaInstanceState () method can be used to save the state information, there is also a restriction that you can only save your information through the Bundle object. This method is not suitable if you want to save more and more complex data structures.
Another method that can be used is the onRetainNonConfigurationInstance () callback method. When an activity is destroyed due to configuration changes (such as screen orientation changes), the Android system calls this method. This can be used as follows:
[Java]
@ Override
Public Object onRetainNonConfigurationInstance (){
// --- Save whatever you want here; it takes in an Object type ---
Return ("Some text to preserve ");
}
Return the information you want to save.
It can be observed that this method returns an Object, which allows you to return any data type. If you want to extract the stored data, you can extract it in the onCreate () method and use the getLastNonConfigurationInstance () callback method:
[Java]
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Log. d ("StateInfo", "onCreate"); www.2cto.com
String str = (String) getLastNonConfigurationInstance ();
}
The onRetainNonConfigurationInstance () and getLastNonConfigurationInstance () methods allow you to save information at any time.
For example, when a user downloads data and changes the screen method, it is better to re-download the data by using these two methods to save the data.