In Fragment, use onSaveInstanceState to save data and restore data in onActivityCreated.
public void onActivityCreated(Bundle savedInstanceState) { (savedInstanceState); ... if (savedInstanceState != null) { // Restore the fragment's state here } } public void onSaveInstanceState(Bundle outState) { (outState); // Save the fragment's state here }
In Activity, use onSaveInstanceState to save the data of the Fragment instance and restore the data in onCreate:
private Fragment myFragment; public void onCreate(Bundle savedInstanceState) { ... if (savedInstanceState == null) { // Instantiate fragment } else { // Restore the fragment's instance myFragment = getSupportFragmentManager().getFragment( savedInstanceState, "fragment"); } ... } @Override protected void onSaveInstanceState(Bundle outState) { (outState); //Save the fragment's instance // fragment instance may be null if (myFragment != null) { getSupportFragmentManager().putFragment(outState, "fragment", myFragment); } }
Example
First look at a step. If you close the Activity or Fragment manually, for example, click the back key and actively close the current page, you will go onPause() --> onStop() --> onDestroy() and onSaveInstanceState() will not be called. onSaveInstanceState() will only be called before the system is about to automatically clean up and destroy the Activity or Fragment, for example
1. Due to gravity sensing, the phone changes from vertical screen to horizontal screen,
2. Click the Home button on your phone and press the Home button for a long time
3. When clicking 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.
Based on the above situation, it can be seen that onSaveInstanceState() This method is suitable for temporarily saving some non-permanent data. If you want to save data persistently, you must place the operations in methods such as onStop() and onDestroy(). onSaveInstanceState() It is suitable for the current Activity or Fragment that is destroyed by the system itself, and the application can save some necessary data before this, and the user's operation will quickly return to the current page. At this time, the data is not lost, and the previous state can be restored to the greatest extent. This is the greatest significance of this method. The following example is a simple example:
In the Fragment:
//The system saves necessary data before automatically destroying the Fragment@Override public void onSaveInstanceState(Bundle outState){ <span style="white-space:pre"> </span>(outState); <span style="white-space:pre"> </span>String content = ().toString(); <span style="white-space:pre"> </span>("inputCon", content); } //Recover data@Override public void onViewStateRestored(Bundle savedInstanceState){ <span style="white-space:pre"> </span>(savedInstanceState); <span style="white-space:pre"> </span>//Restore the contents of the previous input box<span style="white-space:pre"> </span>if(savedInstanceState != null){ <span style="white-space:pre"> </span>(("inputCon", "")); } }
In Activity
//The system saves necessary data before automatically destroying the activity@Override public void onSaveInstanceState(Bundle outState){ <span style="white-space:pre"> </span>(outState); String content = ().toString(); ("inputCon", content); } //The data can be recovered in the onRestoreInstanceState() method in the Activity, or it can be restored in onCreate(), because once the Activity is destroyed, it will rewind the normal life cycle starting with onCreate() once it starts.@Override public void onRestoreInstanceState(Bundle savedInstanceState){ (savedInstanceState); //Restore the contents of the previous input box if(savedInstanceState != null){ (("inputCon", "")); } } @Override public void onDestroy(){ (); //The last data can be saved before the Activity is destroyed, and there will be no store after passing this village.}
There is an important point here. The onSaveInstanceState() method mentioned above will only be called when the Activity or Fragment is determined to be automatically cleared by the system. If it is not a long-term background application and the page has not been killed by the system, the corresponding Activity or Fragment will not call onSaveInstanceState() methods.