Challenge: Saving State

Figure out how to persist your boxes across orientation changes from within your View. This can be done with the following View functions:

    protected fun onSaveInstanceState(): Parcelable
    protected fun onRestoreInstanceState(state: Parcelable)

These functions do not work like Activity and Fragment’s onSaveInstanceState(Bundle). First, they will only be called if your View has an ID. Second, instead of taking in a Bundle, they return and process an object that implements the Parcelable interface.

We recommend using a Bundle as the Parcelable instead of implementing a Parcelable class yourself. Kotlin has a @Parcelize annotation that makes it easier to create a Parcelable class, but the Bundle is a commonly ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.