I'm trying to save a current state of the ViewPager
(current position etc.) by implementing two methods in its PagerAdapter
: restoreState()
and saveState()
. However they seem to not work correctly in my case. What I'm doing wrong?
@Override
public Parcelable saveState() {
Bundle bundle = new Bundle();
bundle.putParcelable("instanceState", super.saveState());
bundle.putInt("stateToSave", position);
return bundle;
}
@Override
public void restoreState(Parcelable state, ClassLoader c) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
position = bundle.getInt("stateToSave");
super.restoreState(bundle.getParcelable("instanceState"), c);
return;
}
super.restoreState(state, c);
}