setRetainInstance不保留实例

时间:2012-08-19 12:22:59

标签: java android android-fragments

我正在尝试使用setRetainInstance(),但它似乎对我不起作用! =(。 我将问题简化为最简单的版本:

我有一个带有TextView和Button的片段。

当按下按钮时,TextView的初始文本是“我正在等待”,文本变为“你好!”

那是我的代码:

片段:

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>

但是当我播放它时,按下按钮并更改设备的方向,文本将重置。

我错过了什么?我该如何解决?谢谢!

1 个答案:

答案 0 :(得分:10)

您错过了再次轮换后onCreateView运行并再次使用默认文本膨胀默认视图。

您需要手动恢复视图状态,例如:

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

}