0

used intent from a button click to go to another activity and then later return back to the original activity.

i tried to save two integers on leaving the activity for the other activity, then later when i return back to the original activity i get a null for the bundle.

does anyone know why this is not working?

i used savedInstanceState bundle to save variables on screen rotation and it works in this situation, however when i leave to view another activity and later return back to this activity the bundle was not saved. getting null on return.

the onSavedInstanceState method

  @Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);

outState.putInt("index", index);
outState.putInt("top", top);

} // end onSavedInstanceState

the first part of the onCreate method containing the code for getting the savedInstatnceState saved variables

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inspectioninfoselectionlist);

    if(savedInstanceState!=null){
        index = savedInstanceState.getInt("index");
        top = savedInstanceState.getInt("top");
        Log.i("$$$$$$$", "value of index and top returned after activity starts" + index +  " " + top);
    }else if (savedInstanceState == null){
        Log.i("$$$$$$$", "saved instance state is null" + index +  " " + top);
    }
Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
Kevik
  • 9,181
  • 19
  • 92
  • 148

1 Answers1

1

onSaveInstanceState() is called when a configuration change occurs, such as a screen orientation, change in language, etc.
Opening another activity is not a configuration change, that is why the method is not called.

Andy Res
  • 15,963
  • 5
  • 60
  • 96