我知道如何保存和恢复Activity的状态。 但是我想要做的是保存和恢复视图的状态。 我有一个自定义视图和其中两个重写方法:
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
currentLeftX = bundle.getInt(CURRENT_LEFT_X_PARAM, 0);
currentTopY = bundle.getInt(CURRENT_TOP_Y_PARAM, 0);
}
super.onRestoreInstanceState(state);
}
@Override
protected Parcelable onSaveInstanceState() {
super.onSaveInstanceState();
Bundle bundle = new Bundle();
bundle.putInt(CURRENT_LEFT_X_PARAM, currentLeftX);
bundle.putInt(CURRENT_TOP_Y_PARAM, currentTopY);
return bundle;
}
我原以为它会无缝运行,但遇到了错误:
造成的原因是: java.lang.IllegalArgumentException: 错误的状态类,期望View 状态,但收到类 android.os.Bundle。当两个不同类型的视图在同一层次结构中具有相同的ID时,通常会发生这种情况。此视图的ID为 id/mapViewId,请确保其他视图不使用相同的ID。 at android.view.View.onRestoreInstanceState(View.java:6161)
但是这个视图是我活动中唯一的视图。所以我想问:
保存视图状态的正确方法是什么?