删除后片段仍然存在?

时间:2016-01-04 06:29:13

标签: android fragment

1将两个片段添加到FragmentManager

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft1 = fm.beginTransaction();
ft1.add(containerId, fragment1, "fragment1");
ft1.addToBackStack(null);
ft1.commitAllowingStateLoss();

FragmentTransaction ft2 = fm.beginTransaction();
ft2.add(containerId, fragment2, "fragment2");
ft2.addToBackStack(null);
ft2.commitAllowingStateLoss();

2更改移动设置     开发者选项 - >不要保持活动 - > ON

3在重新创建活动后删除所有片段

FragmentTransaction ft3 = fm.beginTransaction();
for(Fragment f : fm.getFragments())  ft3.remove(f); 
ft3.commitAllowingStateLoss();

我的问题是为什么fm.findFragmentByTag(“fragment1”)在删除后不为空?

minSdkVersion 17  targetSdkVersion 22  compileSdkVersion 22

8 个答案:

答案 0 :(得分:8)

尝试使用此代码删除片段

FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(myFrag);
trans.commit();
manager.popBackStack();

我认为您还没有添加此方法

manager.popBackStack();

getSupportFragmentManager().popBackStack()

答案 1 :(得分:2)

使用popBackStackImmediate()代替popBackStack(),确保片段立即从backstack中删除。横断移除方法不会从后台拆除碎片,只是为了横切。

答案 2 :(得分:0)

而不是

ft1.add(containerId, fragment1, "fragment1");

尝试

ft1.replace(containerId, fragment1, "fragment1");

并使用

fragmentmanager.popBackStack();

答案 3 :(得分:0)

你应该commit个别片段删除交易。

此代码将在循环完成后提交

for(Fragment f : fm.getFragments())  ft3.remove(f); 
ft3.commitAllowingStateLoss();

而不是使用brackets

for(Fragment f : fm.getFragments()){
 ft3.remove(f); 
 ft3.commitAllowingStateLoss();
}

答案 4 :(得分:0)

试试这个......

    List<Fragment> list = getSupportFragmentManager().getFragments();
    if (list == null) {
        Log.e("TAg", "No existing fragments" );
    }

    for (Fragment frag : list)
    {
        // To save any of the fragments, add this check.
        // A tag can be added as a third parameter to the fragment when you commit it
        if (frag.getTag().equals("tag-name")) {
            continue;
        }

        getSupportFragmentManager().beginTransaction().remove(frag).commit();
    }

答案 5 :(得分:0)

          public  void popAll() {
            if (mFragmentManager != null) {
                if (!mOnPause && mFragmentManager.getBackStackEntryCount() > 0) {
                    if (isFragmentOnStack(HomeFragment.TAG_NAME))
                        mFragmentManager.popBackStackImmediate(HomeFragment.TAG_NAME, 0);
                    else
                        mFragmentManager.popBackStackImmediate(null,
                                FragmentManager.POP_BACK_STACK_INCLUSIVE);
                } else if (mFragmentManager.getBackStackEntryCount() > 0) {
                    mTransactionList.clear();
                    mTransactionList.add(new FragmentPopAll());
                }


 }
    }

答案 6 :(得分:0)

我遇到了同样的问题。我通过使用标签删除了片段,如下所示:

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentByTag("fragment1");
ft.remove(fragment);
ft.commit();

现在findFragmentByTag()应该返回null

答案 7 :(得分:0)

使用commitNow方法代替commitAllowingStateLoss,因为它可以同步执行事务