2

在我的 CustomPageAdapter 类中,我实现了这两种方法来恢复 viewPager 状态。为了测试它,为了简单起见,我尝试传递整数并检索它,但是我仍然得到 0。这是方法实现:

 @Override
 public void restoreState(Parcelable state, ClassLoader arg1) {
 if (state instanceof Bundle) {
    Bundle bundle = (Bundle) state;
    saved = bundle.getInt("stateToSave");
    super.restoreState(bundle.getParcelable("instanceState"), arg1);
    return;
 }
 super.restoreState(state, arg1);
 }

 @Override
 public Parcelable saveState() {
    Bundle bundle = new Bundle();
    bundle.putParcelable("instanceState", super.saveState());
    bundle.putInt("stateToSave", 8);
    return bundle;
 }
4

0 回答 0