旋转后无法更新动态创建的片段

时间:2013-06-12 17:02:22

标签: android android-layout rotation fragment android-support-library

我有片段和配置更改(旋转)的问题:我有一个带有按钮的屏幕,该按钮随机更新片段的文本。

一切正常,直到我旋转设备(配置更改):我可以findFragmentByTag片段,但其getView()方法返回null。 我正在使用setRetainState(true):所有私有字段都很好地保留。

在下面的代码中,我正在动态创建片段,因为我在视图分页器的上下文中(TestItemListFragment引用ViewPager页面的片段,TestItemDetailFragment引用我希望文本更新的片段)点击按钮时)。但是,对于此示例,显示的代码已简化,请参阅代码中的注释以获取详细信息。

我错过了什么?我究竟做错了什么 ? (当然我是)

编辑链接到复制来源:https://bitbucket.org/bixibu/demo-nestedfragment-android

活动:

public class TestActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();

        Bundle args = new Bundle();
        TestItemListFragment frag = TestItemListFragment.newInstance();

        if (getSupportFragmentManager().findFragmentByTag("tag") == null) {
            getSupportFragmentManager().beginTransaction().add(android.R.id.content, frag, "tag").commitAllowingStateLoss();
        }
    }
}

页面片段:

public class TestItemListFragment extends Fragment {


    ItemDetailFragment detailFragment = null;
    String mCurTitle = null;

    private SecureRandom random = new SecureRandom();

    public static TestItemListFragment newInstance() {
        TestItemListFragment fragment = new TestItemListFragment();
        return fragment;
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        return inflater.inflate(R.layout.item_list_fragment_test, container, false);
    }

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

        /**
         * Addind random string generation while clicking on button
         * And setting this random String to the fragment
         */
        Button button = (Button) getView().findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                updateDetail(new BigInteger(130, random).toString(32));
            }
        });

        // creating new fragment
        detailFragment = ItemDetailFragment.newInstance(new Bundle());

        // in my XML I don't have any ID because I have to create as many fragment as I have Pages in my ViewPager
        // so I dynamically creates an ID
        // in order to have a different fragment for each Page
        // if I don't do that only my first Page display a fragment (or multiple ones)
        int fragmentContainerUniqId = 519618565;
        View fragmentContainer = getView().findViewById(R.id.detailFragmentContainer);
        fragmentContainer.setId(fragmentContainerUniqId);

        if (getActivity().getSupportFragmentManager().findFragmentById(fragmentContainerUniqId) == null) {
            // Add the fragment to the 'detailFragmentContainer' FrameLayout
            getActivity().getSupportFragmentManager().beginTransaction()
                    .add(fragmentContainerUniqId, detailFragment).commitAllowingStateLoss();
        }

        if (mCurTitle != null) {
            updateDetail(mCurTitle);
        }

    }

    // May also be triggered from the Activity
    public void updateDetail(String title) {
        detailFragment.setTitle(title);
    }
}

细节片段:

public class ItemDetailFragment extends Fragment {

    public static ItemDetailFragment newInstance(Bundle bundle) {
        ItemDetailFragment fragment = new ItemDetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.item_detail_fragment, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        setTitle(getArguments().getString("title"));
    }

    public void setTitle(String title) {
        if (getView() != null && title != null) {
            TextView view = (TextView) getView().findViewById(R.id.detailsText);

            view.setText(title);
        }
    }
}

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="horizontal" >

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


        <FrameLayout
            android:id="@+id/detailFragmentContainer"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2" />


</LinearLayout>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.burnside.digital.nestedfragments"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.burnside.digital.nestedfragments.TestActivity"
            android:label="@string/title_activity_test" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

ps:我正在使用support-v4 lib

感谢您的帮助,我会更新我的帖子,我不清楚,所以请告诉我。

2 个答案:

答案 0 :(得分:2)

屏幕旋转后,您的片段(屏幕上显示)不会更新,因为它与detailFragment不同。

屏幕旋转后,您将创建一个新片段:

   detailFragment = ItemDetailFragment.newInstance(new Bundle());

但它不会添加到fragmentContainerUniqId,因为

   getActivity().getSupportFragmentManager().findFragmentById(fragmentContainerUniqId)

不为空

由此更改TestItemListFragment中的代码,片段将更新

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

    /**
     * Addind random string generation while clicking on button
     * And setting this random String to the fragment
     */
    Button button = (Button) getView().findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            updateDetail(new BigInteger(130, random).toString(32));
        }
    });


    // in my XML I don't have any ID because I have to create as many fragment as I have Pages in my ViewPager
    // so I dynamically creates an ID
    // in order to have a different fragment for each Page
    // if I don't do that only my first Page display a fragment (or multiple ones)
    int fragmentContainerUniqId = 519618565;
    View fragmentContainer = getView().findViewById(R.id.detailFragmentContainer);
    fragmentContainer.setId(fragmentContainerUniqId);

    if ((detailFragment = (ItemDetailFragment) getActivity().getSupportFragmentManager().findFragmentById(fragmentContainerUniqId)) == null) {
        // Add the fragment to the 'detailFragmentContainer' FrameLayout
        // creating new fragment
        detailFragment = ItemDetailFragment.newInstance(new Bundle());
        getActivity().getSupportFragmentManager().beginTransaction()
                .add(fragmentContainerUniqId, detailFragment).commitAllowingStateLoss();
    }

    if (mCurTitle != null) {
        updateDetail(mCurTitle);
    }
}

答案 1 :(得分:0)

在PageFragment中,而不是:

    getActivity().getSupportFragmentManager();

使用:

    getSupportFragmentManager();

旋转后,第一个表单仍将引用原始活动。 第二个表单将正确引用新创建的活动。