4

I am trying to remove a fragment from my stack. I am using this code:

FragmentManager fm = getSupportFragmentManager();
    if (fm != null) {
        FragmentTransaction ft = fm.beginTransaction();
        Fragment currentFragment = fm.findFragmentById(R.id.my_id);
        if (currentFragment != null) {
            ft.remove(currentFragment);
            ft.commit();
        }
    }

Do I need to call popBackStack() with the above code?

fm.popBackStack();
user3773337
  • 2,086
  • 4
  • 20
  • 29
  • I would take a look at this answer: http://stackoverflow.com/questions/17793249/how-do-popbackstack-and-replace-operations-differ – Petro Apr 11 '15 at 01:14

1 Answers1

5

remove() will simply remove the fragment.

popBackStack() will remove current fragment and replace it with the last one from the stack. For this to work you need to do addToBackstack() on the last fragment transaction. But this is not what you want to do so don't do it.

inmyth
  • 8,880
  • 4
  • 47
  • 52