Tôi đang cố gắng lưu trạng thái hiện tại của ViewPager
(vị trí hiện tại, v.v.) bằng cách triển khai hai phương thức trong số PagerAdapter
: restoreState()
và saveState()
. Tuy nhiên chúng dường như không hoạt động chính xác trong trường hợp của tôi. Tôi đang làm gì sai?Tại sao restoreState() và saveState() của PagerAdapter sẽ không hoạt động?
@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);
}
Bạn không nên gọi onSaveInstanceState và onRestoreInstanceState mình. Hãy để nó vào khung công tác Android để làm như vậy. –