I am trying to save the current state ViewPager(current position, etc.) by implementing two methods in PagerAdapter: restoreState()and saveState(). However, it seems they do not work correctly in my case. What am I 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);
}
source
share