Why don't restoreState () and saveState () for the PagerAdapter work?

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);
}
+5
source share
1 answer

Have you tried to use the onSaveInstanceState method from ViewPager? In your activity / fragment, you can save the ViewPager state by calling this method and passing the result to the Bundle.

After restoring the call to Activity / Fragment onRestoreInstanceState (Parcelable p) with

-1
source

All Articles