我正在使用fragmentTransaction进行片段,看起来像这样:
FragmentManager fm = oldFragment.getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(oldFragment.getId(), newFragment, tag);
fragmentTransaction.hide(oldFragment);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
有几个片段,我想跳过一些片段。
示例:
frag1->(按NEXT) -> frag2->(按下一个) -> frag3->(压后) -> frag1
当我使用替换而不是添加和隐藏
时,此解决方案有效getFragmentManager().popBackStack(
tag,
FragmentManager.POP_BACK_STACK_INCLUSIVE);
参考"跳过一些碎片onbackpressed
但就我而言,跳过片段短暂显示。我该如何使它好像之间的片段从未存在?
我使用此功能去除片段告诉我是否有帮助。
public static void removeFragment(FragmentActivity activity) {
try {
FragmentManager supportFragmentManager = activity.getSupportFragmentManager(); // getFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_down, R.anim.slide_up);
fragmentTransaction.remove(supportFragmentManager.getFragments().get(0));
try {
fragmentTransaction.commit();
} catch (Exception e) {
}
supportFragmentManager.popBackStack();
} catch (NullPointerException exception) {
exception.printStackTrace();
}
}