Basic part of Android re-Learning-saving activity status
Learning the lifecycle of an Activity, we know that when the Activity enters the paused or stopped status, the status of the Activity is still saved. Because this Activity object is still stored in the memory, all its information and status still exist. When this Activity is returned to the foreground again, it will remain as it leaves.
However, if the Activity enters the background, the system may destroy the Activity in order to recycle the memory. When the user returns through the navigation, the system cannot simply restore the Activity, instead, the Activity object is re-created. However, for an ordinary user, he does not know that the system has destroyed the Activity and re-created it. Therefore, he may want the Activity to be saved in the same status as when he left.
In this case, you can use the callback method onSaveInstanceState () to save some important information about the Activity, and use the onCreate () or onRestoreInstanceState () callback method to restore the information.
- @ Override
- ProtectedvoidonSaveInstanceState (BundleoutState ){
- Super. onSaveInstanceState (outState );
- Log. d ("roamer", "onSaveInstanceState ");
- }
-
- @ Override
- ProtectedvoidonCreate (BundlesavedInstanceState ){
- Super. onCreate (savedInstanceState );
- SetContentView (R. layout. activity_main );
- Log. d ("roamer", "onCreate ");
- }
-
- @ Override
- ProtectedvoidonRestoreInstanceState (BundlesavedInstanceState ){
- Super. onRestoreInstanceState (savedInstanceState );
- Log. d ("roamer", "onRestoreInstanceState ");
- }
The Bundle object passed in through the onSaveInstanceState () method is saved to what we want to save. Then, when the Activity is re-created, we can use the Bundle object of the onCreate () or onRestoreInstanceState () method to obtain the previously stored data. As for the usage of Bundle, I will not talk about it much.
The process is as follows:
Note: Not every Activity is destroyed. The system calls back the onSaveInstanceState () method. If we finish the Activity ourselves, the onSaveInstanceState () method will not be called.
In addition, the time when the system calls the onSaveInstanceState () method is generally before onStop (), but may also be before onPause.
The time when the system calls onRestoreInstanceState () is after onStart () and before onResume.
The default mechanism for saving Activity status. However, even if we do not override the onSaveInstanceState () method, the default implementation of Activity for the onSaveInstanceState () method will still help us restore some states. Specifically, the default implementation will call the onSaveInstanceState () method of each View in the Activity layout. The onSaveInstanceState () method in View is as follows:
- // OnRestoreInstanceState () method of View
- // This method allows each View to provide the information you want to save
- @ Override
- ProtectedvoidonRestoreInstanceState (Parcelablestate ){
- // TODOAuto-generatedmethodstub
- Super. onRestoreInstanceState (state );
- }
Almost every widget in the Android framework implements this method as appropriate, so that any visible UI change activities are automatically saved and restored upon re-creation. For example, the EditText part stores any text entered by the user, and the CheckBox part stores whether it is selected. The only task we need to do is to provide a unique ID (android: ID attribute) to save the status of each widget. If a part does not have an ID, the system cannot save its status. Of course, if you do not need a View to automatically save and restore its status in some special circumstances, you can set the View attribute android: saveEnabled to false, or call the setSaveEnabled () method.
Although the system helps us to automatically save and restore the UI, we sometimes need to save and restore some other member variables. In this case, remember to rewrite the onSaveInstanceState () and onRestoreInstanceState () methods. Remember to add the implementation of the super method, because it is the default Implementation of Automatic saving and restoration of the system UI components.
Note: Because the onSaveInstnceState () method is not executed before the Activity is destroyed, it is recommended that you only save information related to the Activity status (UI status ). You should not store the persistent data. The persistent data should be stored in the onPause Method for execution (such as storing the data in the database ).
The method to test the Activity State Preservation the time when the Activity calls the onSaveInstanceState () method is fine, but it is a bit difficult to control when the onRestoreInstanceState () is called, because it is difficult to control when the system memory is insufficient to destroy the Activity. So we can use the following method to test the Activity status saving. The rotation setting changes the screen direction (the premise is that you have not locked the rotation function and the Activity has not set the screen direction ). When the screen direction changes, the system will destroy and recreate the Activity to find alternative resources for the new screen configuration. There are three main problems for saving the activity status: 1. When is the onSaveInstanceState method executed, because it is not mandatory or necessary in the activity lifecycle?
OnSaveInstanceState () is called only when the system is about to automatically clear and destroy the Activity or Fragment,
In other words, if you finish it yourself, it will certainly not be called. It must be triggered by the system itself,
For example
1. Because the gravity sensor mobile phone changes from a portrait screen to a landscape screen,
2. Click Home and press Home.
3. When you click the power button to lock the screen
4. jump from the current Activity to another Activity.
5. When the application memory is insufficient, it will be automatically destroyed soon.
2. onSaveInstanceState: whether the stored data is held or permanent. onSaveInstanceState () is suitable for temporarily saving some non-permanent data. if you want to persistently Save the data, you need to put the operation in the onStop (), onDestroy () methods. In this way, the role of onSaveInstanceState () is: onSaveInstanceState () it is suitable that once the current Activity or Fragment is destroyed by the system itself, the application can save some necessary data before this, and the user's operations will soon return to the current page, at this time, the data is not lost, and the previous state can be restored to the maximum extent. this is the biggest significance of this method. 3. the problem of how to re-write, that is, how to save your own data, uses the bundle mode, that is, the key-value mode, because in the android design, the main function of this function is to save the UI state. How can we save some of our own data now:
@Overridepublic void onSaveInstanceState(Bundle outState){ super.onSaveInstanceState(outState); String content = etCon.getText().toString(); outState.putString("inputCon", content);}Obtain:
@ Overridepublic void onViewStateRestored (Bundle savedInstanceState) {super. onViewStateRestored (savedInstanceState); // restore the content in the input box if (savedInstanceState! = Null) {etCon. setText (savedInstanceState. getString ("inputCon ",""));}}In addition to switching between the portrait and landscape screens, status saving can be used to solve the problem of Landscape Display during fragment replacement.