Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fragmentManager.getBackStackEntryCount() does not decrease when removing fragment from manager

The correct behavior I'd like to have is:

Fragment A -> Fragment B -> Fragment C -> Fragment A

What I currently do:

  1. When I want to go back from B to A, I use popBackStack() still here everything goes well.
  2. When I want to go from B to C I remove B and add C. (number of Fragments 2, number of backStackEntryCount 3, same for replace).
  3. When I want to go back from C to A, I can use popBackStack() but the BackStackEntryCount will still contain the entry for B.

I really need the backStackEntryCount to be the same as the fragments contained in the manager.

Anyone know what I am doing wrong?

My code:

        Fragment fragment1 = fragmentManager.findFragmentByTag("NavigationFragment_");
    if (fragment1 != null) {
        fragmentManager.beginTransaction()
                .setTransition(TRANSIT_FRAGMENT_FADE)
                .remove(fragment1)
                .commit();

    }

    fragmentManager.beginTransaction()
            .addToBackStack(backstack)
            .setTransition(TRANSIT_FRAGMENT_OPEN)
            //.remove(getFragmentManager().findFragmentByTag(NavigationFragment_.class.getSimpleName()))
            .add(R.id.fragmentContainer, fragment, fragment.getClass().getSimpleName())
            .commit();

I have been searching for a solution for a while without results, so please do not mark this a duplicate.

Thanks in advance.

like image 671
Jonathan I Avatar asked Nov 08 '22 00:11

Jonathan I


1 Answers

If you come here because you call popBackStack() but getBackStackEntryCount() doesn't seem to update, then you might want to try popBackStackImmediate() instead.

popBackStack() is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.

like image 56
Johan Franzén Avatar answered Nov 14 '22 21:11

Johan Franzén