简体   繁体   中英

When onSaveInstanceState is called in fragment?

In my project I use fragment named PassengerInformation.java. In that fragment I use onSaveInstanceState and onCreate method.In onCreate method I supposed to clear sharedPreferences if there is no bundle passed from onSaveInstanceState, if onSaveInstanceState pass a bundle I would not need to clear sharedPreferences. My question is when I press back, onSaveInstanceState is not called I supposed to log it but log does not print anything.

Thanks

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //clear sharedpreferences PassengerInformation is firsttime loaded
    if( savedInstanceState == null ) {  
        Log.i("clear PassengerInformation", "sharedPreferences clear");
        edit.clear().commit();
    }       
}
@Override
public void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    outState.putBoolean("clearPreferences", false);
    Log.i("saveInstanceState PassengerInformation", "save");
}

Generally, When you change the orientation from portrait to landscape, save instance state is called.

For eg: you are displaying a result, if user changes orientation, you need to display the same result means,

onresume -> SavedinstanceState( where you will use key and value to store the data)-> destroy

and onCreate-> Restore savedInstanceState( Where you will use method getExtra() of the key and display the result where you stored in restoreSavedInstance state.

You can Read a good explanation from this

eg: public static final String key="Hello";

  // you can get the data here.
 @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
    savedInstanceState.getString(key); 
    Toast.makeText(this, "Restore state"+ savedInstanceState.getString(key), Toast.LENGTH_SHORT)
    .show();
    text.setText(key); // here the output will be Good morning.

}
// orientation change where from on resume-> destroy state, if you need to handle any saved data
 @Override
protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
     Toast.makeText(this, "saved state", Toast.LENGTH_SHORT)
        .show();
    outState.putString(key, "Good morning");

}

onSaveInstanceState() is not supposed to be called when the user presses BACK.

REASON:

If you are in your activity and you hit Back button on the device, your activity is finish()ed and next time you start your app it is started again (it sounds like re-created, isn't?) but this time without saved state because you intentionally exited it when you hit Back button.

It will be called when the user changes configuration (eg, rotates the screen), if Android believes that the activity is at risk of being destroyed while still being reachable on the stack, and perhaps a few other cases.

Read - onSaveInstanceState when click back button in Android

Fragment onSaveInstanceState(@NonNull Bundle outState) is called in following cases

1) When screen orientation is changed

2) When you use FragmentStatePagerAdapter : In this case default OffscreenPageLimit is 1. While swiping Fragments in ViewPager if you have crossed OffscreenPageLimit and you have used FragmentStatePagerAdapter , fragments outside this limit will be destroyed and detached ie onStop(), onSaveInstanceState(), onDestroyView(), onDestroy() annd onDetach() would be executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM