我知道活動狀態保存和恢復。 但我想要做的是保存和恢復視圖的狀態。 我有一個自定義視圖和兩個overrided方法是:保存和恢復視圖狀態android
@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;
}
我預計這個工作無縫的,但遇到和錯誤:
Caused by: java.lang.IllegalArgumentException: Wrong state class, expecting View State but received class android.os.Bundle instead. This usually happens when two views of different type have the same id in the same hierarchy. This view's id is id/mapViewId. Make sure other views do not use the same id. at android.view.View.onRestoreInstanceState(View.java:6161)
但這種觀點在我的活動唯一的一個。所以,我問:
什麼是正確的方式來保存視圖的狀態?
我不得不因爲Android的的設計多麼可笑的笑。該方法不期望顯示狀態***顯然***,它***顯然***期望名爲*** Parcelable ***的接口。是的,我們確實返回一個有效的*** Parcelable ***,它由*** Bundle ***實施。但是由於荒謬的原因失敗了。我從來沒有在.NET中遇到過這種設計,當它期望一個接口時,如果我們返回確切的接口,就不應該有任何異常。例外情況應該說更有意義的東西=)))Android是***糟糕的設計可以讓你惱火的一個很好的例子。 – 2016-10-12 02:41:01