Method of Saving Fragment State with onSaveInstanceState in Android

  • 2021-07-06 11:53:50
  • OfStack

In Fragment, data is saved by onSaveInstanceState, and data can be recovered by onActivityCreated.


public void onActivityCreated(Bundle savedInstanceState) {
  super.onActivityCreated(savedInstanceState);
  ...
  if (savedInstanceState != null) {
    // Restore the fragment's state here
  }
}

public void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  // Save the fragment's state here
}

In Activity, the data of Fragment instance is saved by onSaveInstanceState, and the data can be recovered 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) {
  super.onSaveInstanceState(outState);
  //Save the fragment's instance
  // fragment instance may be null
  if (myFragment != null) {
    getSupportFragmentManager().putFragment(outState, "fragment", myFragment);
  }
}

Instances
First look at one step. If you manually close Activity or Fragment, such as clicking the Back button and actively closing the current page, you will go onPause ()- > onStop() -- > onDestroy () and onSaveInstanceState () will not be called. onSaveInstanceState () will only be called before the system will automatically clean up and destroy Activity or Fragment, such as

1. Because the gravity sensing mobile phone changes from vertical screen to horizontal screen,

2. Click the Home key on the mobile phone and press the Home key 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 and will be automatically destroyed, etc.


To sum up the above situation, As can be seen, onSaveInstanceState () This method is suitable for temporarily saving some non-persistent data. If you want to persist data, You will put the operation in onStop (), onDestroy () Among these methods, onSaveInstanceState () is suitable for the current Activity or Fragment1 once destroyed by the system itself. The application can also save some necessary data before this, and the user's operation quickly returns 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 Fragment:


// Automatic system destruction Fragment Save the necessary data before 
@Override
public void onSaveInstanceState(Bundle outState){
<span style="white-space:pre"> </span>super.onSaveInstanceState(outState);
<span style="white-space:pre"> </span>String content = etCon.getText().toString();
<span style="white-space:pre"> </span>outState.putString("inputCon", content);
}
 
// Recovery of data 
@Override
public void onViewStateRestored(Bundle savedInstanceState){
<span style="white-space:pre"> </span>super.onViewStateRestored(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>etCon.setText(savedInstanceState.getString("inputCon", ""));
 }
}

In Activity


// Automatic system destruction Activity Save the necessary data before 
@Override
public void onSaveInstanceState(Bundle outState){
<span style="white-space:pre"> </span>super.onSaveInstanceState(outState);
 String content = etCon.getText().toString();
 outState.putString("inputCon", content);
}
 
//Activity Available in onRestoreInstanceState() Method to restore data , It can also be found in onCreate() Middle recovery , Because Activity1 Once destroyed, it will go again 1 Times onCreate() Beginning normal life cycle 
@Override
public void onRestoreInstanceState(Bundle savedInstanceState){
 super.onRestoreInstanceState(savedInstanceState);
 // Restore the contents of the previous input box 
 if(savedInstanceState != null){
 etCon.setText(savedInstanceState.getString("inputCon", ""));
 }
}
 
@Override
public void onDestroy(){
 super.onDestroy();
 //Activity Final can be carried out before being destroyed 1 Saving of secondary data , There will be no such shop after passing this village .
}

There is an important point here. The onSaveInstanceState () method mentioned above will only be called when Activity or Fragment is determined to be automatically cleared by the system. If it is not a long-term background application, the page has not been dropped by the system kill, and the corresponding Activity or Fragment will not call onSaveInstanceState ().


Related articles: