Challenge: Saving State

Box.kt, we make it Parcelable:

class Box(val start: PointF):Parcelable{
var end:PointF = start

val left:Float
    get() = Math.min(start.x, end.x)

val right:Float
    get() = Math.max(start.x, end.x)
val top:Float
    get() = Math.min(start.y, end.y)
val bottom:Float
    get() = Math.max(start.y, end.y)


We assign a ID to the view


and finally we make appropiated changes in our custom view

private const val BOXES = “BOXES”
private const val VIEW_STATE = “VIEW_STATE”

override fun onSaveInstanceState(): Parcelable {

    val state = super.onSaveInstanceState()
    val bundle:Bundle = Bundle()
    bundle.putParcelableArrayList(BOXES, ArrayList<Parcelable>(boxen))
    bundle.putParcelable(VIEW_STATE, state)
    return bundle

override fun onRestoreInstanceState(state: Parcelable) {
    if (state is Bundle){
        boxen = state.getParcelableArrayList<Box>(BOXES)?.toMutableList() ?: mutableListOf()

First of all, you need to add id 'kotlin-parcelize in order to use parcelize annotation feature in kotlin. Second thing, Parcelize anntation won’t work since you provide properties inside class body, instead you should implement Parcelable interface yourself.