BaseSavedState
は Parcelable
なので、 @Parcelize
を使えば簡単に保存処理が書けます。以下は、 num
というパラメータを保存する例です。
override fun onSaveInstanceState(): Parcelable? {
return SavedState(
super.onSaveInstanceState(),
this.num
)
}
override fun onRestoreInstanceState(state: Parcelable?) {
val savedState = state as? SavedState
super.onRestoreInstanceState(savedState?.superState)
savedState ?: return
this.num = savedState.num
}
@Parcelize
private data class SavedState(
val superState: Parcelable?,
val num: Int
) : BaseSavedState(superState)
簡単ですね