4

I am very new to Android (coming from iOS background). I have created a custom/compound view that is simply five buttons inside a merge tagline:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<!--Definition for my custom segmented controller class-->
<Button
    android:id="@+id/button_NA"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:textSize="12sp"
    android:text="@string/button_NA_text"
    android:background="@drawable/button_border"/>
<Button
    android:id="@+id/button_1"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:textSize="12sp"
    android:text="@string/button_1_text"
    android:layout_alignStart="@id/button_NA"
    android:background="@drawable/button_border"/>
<Button
    android:id="@+id/button_2"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:textSize="12sp"
    android:text="@string/button_2_text"
    android:layout_toRightOf="@id/button_1"
    android:background="@drawable/button_border"/>
<Button
    android:id="@+id/button_3"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:textSize="12sp"
    android:text="@string/button_3_text"
    android:background="@drawable/button_border"/>
<Button
    android:id="@+id/button_4"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:textSize="12sp"
    android:text="@string/button_4_text"
    android:background="@drawable/button_border"/>
<Button
    android:id="@+id/button_5"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:textSize="12sp"
    android:text="@string/button_5_text"
    android:background="@drawable/button_border"/>
</merge>

Inside the Kotlin file which inherits from LinearLayout I have implemented:

    override fun onSaveInstanceState(): Parcelable? {
    val bundle = Bundle()

    bundle.putParcelable(STATE_SUPER_CLASS, super.onSaveInstanceState())
    println("Saving segment $mSelectedSegment")
    bundle.putInt(STATE_SELECTED_SEGMENT, mSelectedSegment)

    return bundle
}

override fun onRestoreInstanceState(state: Parcelable) {
    if (state is Bundle) {
        val bundle = state

        super.onRestoreInstanceState(bundle.getParcelable(STATE_SUPER_CLASS))
        val segInt = bundle.getInt(STATE_SELECTED_SEGMENT)
        selectSegment(segInt)
        this.id = bundle.getInt(VIEW_ID)
    } else
        super.onRestoreInstanceState(state)

}

override fun dispatchSaveInstanceState(container: SparseArray<Parcelable>) {
    // Makes sure that the state of the child views in the side
    // spinner are not saved since we handle the state in the
    // onSaveInstanceState.
    super.dispatchFreezeSelfOnly(container)
}

override fun dispatchRestoreInstanceState(container: SparseArray<Parcelable>) {
    // Makes sure that the state of the child views in the side
    // spinner are not restored since we handle the state in the
    // onSaveInstanceState.
    super.dispatchThawSelfOnly(container)
}

The trouble is that if I create and insert this dynamically, then the onRestoreInstanceState() is never called, so I can't restore the value when the screen orientation changes. If I manually insert this custom view into my activity in the XML file, then the function is called and it works like it should.

I am assigning an ID to each view once I create them using View.generateViewId() so they all have unique IDs. I am really at a loss here and have had no luck finding examples concerning saving state with dynamically added views other than this one, but it says that in order to make this work that I would need to manually re-assign the views the IDs that they previously held.

I am not sure how I could accomplish that without just saving each and every dynamically created view, its state, and its ID, and then manually building them again in onCreate...in which case what is the point in onRestoreInstanceState().

Scooter
  • 4,068
  • 4
  • 32
  • 47

0 Answers0