Search code examples
androidkey-value-coding

issue in storing and retrieving stored data in onSavedInstanceState(bundle savedInstanceState)


Consider the below mCreate, etc are int variables they increase dynamically mCreate++. I need to store and retrieve their latest incremented Value. I think I am doing it right but some how they are not applying the latest incremented value.

  @Override
 public void onSaveInstanceState(Bundle savedInstanceState) {

    savedInstanceState.putInt(CREATE_KEY, mCreate);
    savedInstanceState.putInt( RESTART_KEY, mRestart);
    savedInstanceState.putInt(START_KEY, mStart);
    savedInstanceState.putInt(RESUME_KEY, mResume);

    super.onSaveInstanceState(savedInstanceState);

 }

 //////////// get 

  if (savedInstanceState != null) {

       mCreate = savedInstanceState.getInt(CREATE_KEY, mCreate);
       mRestart = savedInstanceState.getInt( RESTART_KEY, mRestart);
       mStart = savedInstanceState.getInt(START_KEY, mStart);
       mResume = savedInstanceState.getInt(RESUME_KEY, mResume);

    }

Solution

  • Code should be like this:

     @Override
     public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
    
        savedInstanceState.putInt(CREATE_KEY, mCreate);
        savedInstanceState.putInt(RESTART_KEY, mRestart);
        savedInstanceState.putInt(START_KEY, mStart);
        savedInstanceState.putInt(RESUME_KEY, mResume);    
     }
    
     if (savedInstanceState != null) {
        mCreate = savedInstanceState.getInt(CREATE_KEY);
        mRestart = savedInstanceState.getInt(RESTART_KEY);
        mStart = savedInstanceState.getInt(START_KEY);
        mResume = savedInstanceState.getInt(RESUME_KEY);
     }