0

I want to remove first (oldest) entry from backstack when back stack entry count more than 3. I've tried something like this

fragmentManager.addOnBackStackChangedListener(() -> {
            int backStackEntryCount = fragmentManager.getBackStackEntryCount();
            if (backStackEntryCount > 3) {
                int firstEntryId = fragmentManager.getBackStackEntryAt(0).getId();
                fragmentManager.popBackStack(firstEntryId, 0);
            }
        });

But it doesn't work as I've expected

Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
flash1512
  • 1
  • 1

2 Answers2

0

There is no way to remove oldest fragment from fragment backstack. There are to two possible solution though: Either every time when you need to remove the oldest fragment from backstack, pop all fragments and push the only needed fragments. Or you may choose to use a custom backstack. You will find tutorials online for custom backstack implementation. Thank you.

Md Golam Rahman Tushar
  • 2,175
  • 1
  • 14
  • 29
0

fragmentManager.remove(fragmentManager.findFragmentById(firstEntryId)).commit()