Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSaveInstanceState() and onRestoreInstanceState(Parcelable state) are not called?

I have a custom view that extends LinearLayout. I have implemented onSaveInstanceState() and onRestoreInstanceState() to save the current view state. However no any action is taken. When I place a log inside of those two methods also nothing appears in Log Cat. I assume that those two methods are not even called. Can anybody explain where is the problem? Thanks.

@Override
public Parcelable onSaveInstanceState() {
    Bundle bundle = new Bundle();
    bundle.putParcelable("instanceState", super.onSaveInstanceState());
    bundle.putInt("currentPage", currentPage);
    return bundle;
}

@Override
public void onRestoreInstanceState(Parcelable state) {

    if (state instanceof Bundle) {
      Bundle bundle = (Bundle) state;
      currentPage = bundle.getInt("currentPage");
      Log.d("State", currentPage + "");
      super.onRestoreInstanceState(bundle.getParcelable("instanceState"));
      return;
    }
       super.onRestoreInstanceState(state);
  }
like image 409
Marcin S. Avatar asked Sep 08 '12 01:09

Marcin S.


People also ask

When is onsaveinstancestate/onrestoreinstancestate called?

But in the cases when the state is saved and restored, and onSaveInstanceState () / onRestoreInstanceState () get called, when exactly are they called? For example, according to the above figures, onRestoreInstanceState () might be called before onStart (), or after onStart () but before onResume (), or after onResume ().

Is onrestoreinstancestate working properly?

It sounds like this is working properly. onRestoreInstanceState is called when your controller has been removed from memory and needs to be restored to its previous state. In your tests this likely never happens.

Why onpause () is called and not onsaveinstancestate (bundle)?

An example when onPause () is called and not onSaveInstanceState (Bundle) is when activity B is launched in front of activity A: the system may avoid calling onSaveInstanceState (Bundle) on activity A if it isn't killed during the lifetime of B since the state of the user interface of A will stay intact. So it's default implementation for..

When is the onstart () method called between onpostcreate ()?

This method is called between onStart () and onPostCreate (Bundle) when the activity is being re-initialized from a previously saved state Show activity on this post.


2 Answers

After digging in android os I have finally figured it out. As I suspected: there is nothing wrong with those two methods. They are just not called. On the Web you can read that 'onRestoreInsatnceState is called when activity is re-created' Ok, it make sens but it's not completely true. Yes, onRestoreInstanceState() is called when activity is recreated but only iff:

it was killed by the OS. "Such situation happen when:

  • orientation of the device changes (your activity is destroyed and recreated)
  • there is another activity in front of yours and at some point the OS kills your activity in order to free memory (for example). Next time when you start your activity onRestoreInstanceState() will be called."

So 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.

like image 135
Marcin S. Avatar answered Nov 15 '22 13:11

Marcin S.


As Steven Byle's comment mentioned, a custom View must have an id assigned to it for onSaveInstanceState to be called. I accomplished this by setting an id in my custom View constructor:

public class BoxDrawingView extends View {
    private int BOX_DRAWING_ID = 555;
    …

    public BoxDrawingView(Context context, AttributeSet attrs) {
        …
        this.setId(BOX_DRAWING_ID);
    }
    …

}
like image 24
Adam Johns Avatar answered Nov 15 '22 14:11

Adam Johns