我在布局中有4个自定义复合组件,每个组件都从RelativeLayout扩展。从概念上讲,它们只是列出带有标题和CheckBox的行。我希望每个组件都保留它自己的检查状态。不幸的是,它们似乎在某种程度上共享它们的状态,即在配置改变之后,所有四个的检查状态由是否仅检查布局中的最后一个来决定。我做了一些日志记录,发现每个唯一id'd行都会调用onSaveInstanceState和onRestoreInstanceState两次。
我很感激任何帮助,这是令人费解的
private int NOT_CHECKED = 0;
private int IS_CHECKED = 1;
@Override
protected Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.checked = mCheckBox.isChecked() ? IS_CHECKED : NOT_CHECKED;
Log.e("Active", "Out - " + this.getId() + " - " + mCheckBox.isChecked());
return ss;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if(!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
mCheckBox.setChecked(ss.checked == IS_CHECKED ? true : false);
Log.e("Active", "In - " + this.getId() + " - " + mCheckBox.isChecked());
}
private static class SavedState extends BaseSavedState {
private int checked;
SavedState(Parcelable superState) {
super(superState);
}
private SavedState(Parcel in) {
super(in);
this.checked = in.readInt();
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(this.checked);
}
public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<FilterCheckBoxRow.SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}