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);
}
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 ().
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.
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..
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.
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:
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.
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);
}
…
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With