如何替换片段存储第一个历史记录并删除其他事务?

时间:2018-03-20 13:34:34

标签: android android-fragments

我有三个片段 a b c
  我用主要活动替换了

FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fram_container, new Fragment1(), "tag1");
transaction.commit();

a b

FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fram_container, new Fragment2(), "tag2");
transaction.commit();

b c

FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fram_container, new Fragment3(), "tag3");
transaction.commit();
c ...

中的

我希望 c 重新按下 a 重定向,而不会在 c 中执行任何操作   我想在后面按 b

我能做什么以及如何做?

1 个答案:

答案 0 :(得分:2)

重新实施以下代码
public void backToFragment(final Fragment fragment) {

    // go back to something that was added to the backstack
    getSupportFragmentManager().popBackStackImmediate(
            fragment.getClass().getName(), 0);
    // use 0 or the below constant as flag parameter
    // FragmentManager.POP_BACK_STACK_INCLUSIVE);

}