4

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

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

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

问题:如果显示 并且设备旋转两次Fragment1,则会丢失其状态。但是,如果设备只旋转一次,一切都会按预期工作。Fragment2

重现步骤:

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

Fragment1失去其状态(复选框未选中且EditText为空)。但是,如果您返回Fragment1step#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" />

片段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>

片段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>

片段测试活动.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();
        }
    }
}

片段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;
    }
}

片段2.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;
    }
}

片段监听器.java

package fragmenttest.example.com;

public interface FragmentListener {
    public void onButtonClick();
}
4

1 回答 1

2

这可能是因为在恢复片段时未调用 onCreate。请参阅:http: //developer.android.com/reference/android/app/Fragment.html#setRetainInstance(boolean)

于 2012-08-20T20:12:34.460 回答