多次调用 FragmentTransaction.replace() - 只有一个在方向改变后有效 [英] Multiple calls to FragmentTransaction.replace() - only one works after orientation change

查看:74
本文介绍了多次调用 FragmentTransaction.replace() - 只有一个在方向改变后有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码用 2 个片段填充我的 UI,容器是在 XML 中定义的 FrameLayout.这是第一次调用此代码,即当应用程序启动时,它工作正常,并且我的两个片段都按预期显示.但是,在配置更改(特别是方向)之后,只会显示事务中的第一个片段.

I am using the following code to populate my UI with 2 fragments, the containers are FrameLayout's defined in XML. This first time this code is called i.e. when the app starts, it works fine, and both my fragments are displayed as expected. However after a configuration change(specifically, orientation), only the first fragment in the transaction is shown.

我不认为这是片段本身的问题,因为如果我颠倒代码,因此在另一个之前调用一个替换,反之亦然,将显示该片段.因此,例如以下面的片段作为指南,如果我交换 mSummary 和 mDetails 替换调用,则将显示 mDetails 而不会显示 mSummary.

I don't think it's an issue with the fragments themselves, because if I reverse the code so one replace is called before the other or vice versa, that fragment will be displayed. So for example with the snippet from below as a guide, if I swap the mSummary and mDetails replace calls, then mDetails will be displayed and mSummary won't.

丢失的总是块中的第二个.

It's always the second one in the block that is missing.

// Using tablet layout
} else {
    FragmentManager fm = super.getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.summary_container, mSummaryFragment);
    ft.replace(R.id.details_container, mDetailsFragment);
    ft.commit();
}

我将片段保存在 onSaveInstanceState 中,并在重新创建 Activity 时从 Bundle savedInstanceState 中恢复它们.我还尝试通过调用 commit() 然后获取另一个 FragmentTransaction 对象将事务分成两部分,但没有任何乐趣.

I'm saving the fragments in onSaveInstanceState and restoring them from the Bundle savedInstanceState when the activity is recreated. I also tried breaking the transaction into two pieces by calling commit() and then getting another FragmentTransaction object but no joy there.

推荐答案

所以对于以后遇到这个问题的任何人...

So for anyone coming across this at a later stage...

我终于通过创建片段的新实例并使用 Fragment.SavedState 对象恢复它的状态来解决这个问题.所以:

I finally manage to fix this by creating a new instance of the fragment and restoring it's state using a Fragment.SavedState object. So:

        if (mSummaryFragment.isAdded() && mDetailsFragment.isAdded()) {
            Fragment.SavedState sumState = getSupportFragmentManager().saveFragmentInstanceState(mSummaryFragment);
            Fragment.SavedState detState = getSupportFragmentManager().saveFragmentInstanceState(mDetailsFragment);

            mSummaryFragment = new SummaryFragment();
            mSummaryFragment.setInitialSavedState(sumState);

            mDetailsFragment = new DetailsFragment();
            mDetailsFragment.setInitialSavedState(detState);
        }

        FragmentTransaction ft = mFragmentManager.beginTransaction();

        ft.add(R.id.summary_container, mSummaryFragment);
        ft.add(R.id.details_container, mDetailsFragment);

        ft.commit();

我不明白为什么这有效,而旧方法无效,但这可能对其他人有帮助.

I do not understand why this works and the old method doesn't, however this may be helpful for someone else.

这篇关于多次调用 FragmentTransaction.replace() - 只有一个在方向改变后有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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