Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
}
like image 532
SAWAUNG Avatar asked Oct 09 '14 08:10

SAWAUNG


2 Answers

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");

}
like image 92
Shadow Avatar answered Oct 16 '22 04:10

Shadow


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 i.e onStop(), onSaveInstanceState(), onDestroyView(), onDestroy() annd onDetach() would be executed.

like image 43
Vikash Kumar Tiwari Avatar answered Oct 16 '22 03:10

Vikash Kumar Tiwari