在我的自定义视图中,我有:
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
return new SavedState(superState, mSomeBoolean);
}
@Override
public void onRestoreInstanceState(Parcelable state) {
final SavedState savedState = (SavedState) state;
super.onRestoreInstanceState(savedState.getSuperState());
this.mSomeBoolean = savedState.someBoolean;
}
我的SavedState
实现BaseSavedState
,问题是看起来writeToParcel(Parcel out, int flags)
没有被调用,但奇怪的是,状态被正确保存。
我不明白为什么会发生这种情况,更重要的是,尽管没有写入someBoolean
,parcel
状态如何得以保存。任何帮助或指导都会有所帮助。
更新
以下是SavedState
实施:
class SavedState extends View.BaseSavedState {
boolean someBoolean;
SavedState(Parcelable superState, boolean someBoolean){
super(superState);
this.someBoolean = someBoolean;
}
private SavedState(Parcel in) {
super(in);
this.someBoolean = (in.readInt() != 0);
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(someBoolean ? 1 : 0);
}
public static final Parcelable.Creator<SavedState> CREATOR
= new Parcelable.Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}