Challenge: Saving State

Box.kt, we make it Parcelable:

@Parcelize
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

<com.bignerdranch.android.draganddraw.BoxDrawingView
android:id="@+id/drawing_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()
        super.onRestoreInstanceState(state.getParcelable(VIEW_STATE))
    }
}