4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

kotlinだとView情報の保存・リストアがParcelizeを使えるので簡単

Posted at

BaseSavedStateParcelable なので、 @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)

簡単ですね

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?