1
我扩展了android.widget.Spinner。并在我的实现中添加了一个int字段。现在我想让它保存方向更改的字段值。我首先想到的是用Bundle对象为:如何保存自定义微调器状态
override fun onSaveInstanceState(): Parcelable {
val bundle = Bundle()
bundle.putParcelable(SUPER_STATE, super.onSaveInstanceState())
bundle.putInt(PREV_ITEM, this.prevItem) // ... save stuff
return bundle
}
override fun onRestoreInstanceState(state: Parcelable?) {
val newState: Parcelable
if (state is Bundle) {
this.prevItem = state.getInt(PREV_ITEM) // ... load stuff
newState = state.getParcelable<Parcelable>(SUPER_STATE)
super.onRestoreInstanceState(newState)
}
super.onRestoreInstanceState(state)
}
但我得到一个错误:
java.lang.ClassCastException: android.os.Bundle cannot be cast to android.widget.Spinner$SavedState
所以我发现微调source code和想通了,我不得不延长内部静态类SavedState并使用它保存我的字段值。但我无法做到这一点。 Android Studio建议它“无法解析符号'保存状态'”。
那么我该如何保存自定义Spinner的状态?
的更好的方法是使用一个静态值,并分配所选择的位置,以静态值和设定在的onResume方法该值。 –