onRestoreInstanceState不重新加载所有视图

时间:2016-08-03 12:08:20

标签: android onrestoreinstancestate

我正在开发一个应用程序,用户可以在其中填写带有照片和一些是/否问题的表单。如果选择是,则需要在对话框中添加额外信息。然后,此信息会插入TextView,并TextView插入LinearLayout

问题是,当用户切换到另一个应用程序时,此屏幕会转到后台,然后被Android破坏(因为系统需要内存)。然后,当用户返回应用程序时,将调用onRestoreInstanceState()方法,返回一些变量,而不是TextView内的LinearLayout。我使用Android注释来返回这些变量。如何恢复这些Android视图?

<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");}

对不起我的初学者英语。 提前谢谢。

3 个答案:

答案 0 :(得分:0)

基于https://developer.android.com/training/basics/activity-lifecycle/recreating.html

您必须保存要返回的变量。 在onSaveInstanceState中保存数据

static final String SAVE_STRING = "string_to_save";
...
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save variable value on bundle
    savedInstanceState.putInt(SAVE_STRING, yourstring);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

然后你可以在onRestoreInstanceState

中检索它们
public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    // Restore state members from saved instance
    yourstring = savedInstanceState.getInt(SAVE_STRING);
}

答案 1 :(得分:0)

据我所知onRestoreInstanceState无法恢复你的观点。 onCreateonRestoreInstanceState使用相同的Bundle,因此请初始化您的观看次数并在savedInstanceState中查看onCreate。检查下面的文档。

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

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

答案 2 :(得分:0)

如果您希望Android将状态自动保存在onSaveInstanceState中,则您的视图需要具有ID