旋转设备两次时片段会失去其状态

Ste*_*viN 5 android android-fragments

我有一个显示两个片段的活动:

  • 创建活动后,会显示Fragment1
  • 当用户按下 上的按钮时Fragment1,它会显示Fragment2,添加到后台堆栈。

片段很简单。Fragment1包含CheckBoxEditTextFragment2包含简单的TextView. 我还调用setRetainInstance(true)了fragment的onCreate(...)方法。

问题:如果显示 且设备旋转两次Fragment1,则会丢失其状态。然而,如果设备仅旋转一次,一切都会按预期进行。Fragment2

重现步骤:

  1. 启动程序。
  2. 检查CheckBox并输入一些文本EditText
  3. Button启动Fragment2
  4. 旋转设备
  5. 再次旋转设备
  6. Back设备(或ESC模拟器)上的按钮返回Fragment1

Fragment1失去其状态(复选框未选中且EditText为空)。然而,如果你回到Fragment1之后step#4-Fragment1则会按预期保持其状态。

哪里有问题?所有代码如下,包括布局。

主要.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/placeholder"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)

片段1.xml

<?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" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </EditText>

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

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

片段2.xml

<?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="Fragment #2" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

FragmentTestActivity.java

package fragmenttest.example.com;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;

public class FragmentTestActivity extends Activity implements FragmentListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FragmentManager fm = getFragmentManager();
        if (fm.findFragmentByTag(Fragment1.TAG) == null) {
            fm.beginTransaction().replace(R.id.placeholder, new Fragment1(), Fragment1.TAG).commit();
        }
    }

    @Override
    public void onButtonClick() {
        FragmentManager fm = getFragmentManager();
        if (fm.findFragmentByTag(Fragment2.TAG) == null) {
            fm.beginTransaction().replace(R.id.placeholder, new Fragment2(), Fragment2.TAG).addToBackStack(Fragment2.TAG).commit();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

片段1.java

package fragmenttest.example.com;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class Fragment1 extends Fragment {
    public static final String TAG = Fragment1.class.getName();
    private FragmentListener mListener;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment1, container, false);

        Button button = (Button) root.findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onButtonClick();
            }
        });

        return root;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        mListener = (FragmentListener) activity;
    }
}
Run Code Online (Sandbox Code Playgroud)

Fragment2.java

package fragmenttest.example.com;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment2 extends Fragment {
    public static final String TAG = Fragment2.class.getName();

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment2, container, false);
        return root;
    }
}
Run Code Online (Sandbox Code Playgroud)

FragmentListener.java

package fragmenttest.example.com;

public interface FragmentListener {
    public void onButtonClick();
}
Run Code Online (Sandbox Code Playgroud)