Search code examples
androidonrestoreinstancestate

onRestoreInstanceState Not Reloading All Views


I'm developing an application where users fill out a form with photos and a few yes/no questions. When yes is selected you need to add extra information in a dialogue. Then this information is inserted into a TextView and this TextView inserted into a LinearLayout.

The problem is, when the user switches to another application this screen goes to background and is later destroyed by Android (because the system needs memory). Then when the user returns to the application, the onRestoreInstanceState() method is called, bringing back some variables but not the TextView inside LinearLayout. I am using Android annotations to return these variables. How do I restore those Android views?

<LinearLayout
                android:id="@+id/estoque_zerado_pergunta_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:visibility="visible">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/existe_estoque_zerado"
                    android:textSize="@dimen/abc_text_size_large_material"
                    android:layout_margin="5dp"/>

                <RadioGroup
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <RadioButton
                        android:id="@+id/radio_estoque_zerado_sim"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/sim" />

                    <RadioButton
                        android:id="@+id/radio_estoque_zerado_nao"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:text="@string/nao"/>

                </RadioGroup>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/estoque_zerado_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:visibility="gone" >

                <LinearLayout
                    android:id="@+id/estoque_zerado_itens_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >

                    <!--Lista de itens aqui-->

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/add_estoque_zerado_item"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:padding="5dp" >

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingRight="10dp"
                        android:src="@drawable/ic_adicionar"
                        android:contentDescription="@string/adicionar_estoque_zerado"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:gravity="center_vertical"
                        android:text="@string/adicionar_estoque_zerado"
                        android:textSize="@dimen/abc_text_size_medium_material" />

                </LinearLayout>

            </LinearLayout>

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    Log.d("onRestoreInstanceState", "onRestoreInstanceState: chamou onRestoreInstanceState");}

    @Override
public void onSaveInstanceState(Bundle bundle) {
    super.onSaveInstanceState(bundle);
    Log.d("onSaveInstanceState", "onSaveInstanceState: chamou onSaveInstanceState");}

Sorry for my beginner English. Thanks in advance.


Solution

  • As i know onRestoreInstanceState does not restore your views. onCreate and onRestoreInstanceState uses same Bundle so initialize your views and check savedInstanceState in onCreate. Check docs out below.

    https://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState(android.os.Bundle)

    https://developer.android.com/training/basics/activity-lifecycle/recreating.html#RestoreState