onSaveInstanceState () and onRestoreInstanceState ()


Question

I'm trying to save and restore the state of an Activity using the methods onSaveInstanceState() and onRestoreInstanceState().

The problem is that it never enters the onRestoreInstanceState() method. Can anyone explain to me why this is?

1
134
10/1/2012 12:14:50 PM

Usually you restore your state in onCreate(). It is possible to restore it in onRestoreInstanceState() as well, but not very common. (onRestoreInstanceState() is called after onStart(), whereas onCreate() is called before onStart().

Use the put methods to store values in onSaveInstanceState():

protected void onSaveInstanceState(Bundle icicle) {
  super.onSaveInstanceState(icicle);
  icicle.putLong("param", value);
}

And restore the values in onCreate():

public void onCreate(Bundle icicle) {
  if (icicle != null){
    value = icicle.getLong("param");
  }
}

You do not have to store view states, as they are stored automatically by calling super.onSaveInstanceState(icicle);.

187
5/26/2016 12:56:41 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon