setRetainInstance不保留实例 [英] setRetainInstance not retaining the instance

查看:194
本文介绍了setRetainInstance不保留实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用setRetainInstance(),但它似乎不是为我工作! =(。 我简化了问题的最简单的版本:

I'm trying to use setRetainInstance() but it seems not working for me! =(. I simplified the problem to the easiest version:

我有一个TextView和一个按钮的片段。

I have a fragment with a TextView and a Button.

在TextView中的初始文本是我在等待按钮时,pressed文本更改为Hello!

The initial text of the TextView is "I'm waiting" when the button is pressed the text changes to "Hello!"

这是我的code:

的片段:

public class SayHelloFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.say_hello_layout, container);
        final TextView text = (TextView) view.findViewById(R.id.textView1);
        view.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                text.setText("Hello!");

            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

}

的活动:

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

}

活动的布局:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/say_hello_fragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    class="com.example.retaintext.SayHelloFragment" />

片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="I&apos;m Waiting"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

但是,当我玩的话,preSS旋钮,改变设备的方向文本变成复位。

But when I play it, press the button and change the device's orientation the text becomes reset.

我在想什么?我该如何解决呢?谢谢!

What am I missing? How can I solve it? Thanks!

推荐答案

您是缺少 onCreateView 再旋转后运行,并再次膨胀时的默认文本的默认视图

You are missing that onCreateView runs after the rotation again and inflates the default view with the default text again.

您需要手动恢复的观点状态,如:

You need to restore the state of your views manually, e.g.:

public class SayHelloFragment extends Fragment {

    private String mText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.say_hello_layout, container);
        final TextView text = (TextView) view.findViewById(R.id.textView1);
        if (mText != null)
            text.setText(mText);

        view.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                mText = "Hello!";
                text.setText(mText);
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

}

这篇关于setRetainInstance不保留实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆