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更改移动设置开发人员选项 - >不要保持活动 - >开启
3重新创建活动后删除所有片段
FragmentTransaction ft3 = fm.beginTransaction(); for(Fragment f : fm.getFragments()) ft3.remove(f); ft3.commitAllowingStateLoss();
我的问题是为什么删除后fm.findFragmentByTag("fragment1")不为null?
minSdkVersion 17 targetSdkVersion 22 compileSdkVersion 22
尝试使用此代码删除片段
FragmentManager manager = getActivity().getSupportFragmentManager(); FragmentTransaction trans = manager.beginTransaction(); trans.remove(myFrag); trans.commit(); manager.popBackStack();
我想你还没有添加这种方法
manager.popBackStack();
要么
getSupportFragmentManager().popBackStack()