Home » Android » android – FragmentManager popBackStack does not work – Bug?

android – FragmentManager popBackStack does not work – Bug?

Posted by: admin June 15, 2020 Leave a comment


I am using this code to clear up my backstack up to my main view:

while(mFragmentManager.getBackStackEntryCount() > 1) {
            mFragmentManager.popBackStack(null, 0);

I am pretty sure this code was working before, but now the backstack count does not change and no Fragment is being removed, which causes an out of memory exception because the while loop keeps running.

Does anyone know, if there is something wrong with it, or if there is a bug in the latest revision of the SDK-tools. I have no clue what’s causing the problem.

How to&Answers:

I certainly would not assume that popBackStack() has an immediate effect, which your loop appears to do.


You are probably looking for popBackStackImmediate(). This immediately executes a back stack pop.


you can also use .executePendingTransactions(); after popBackStack();


What documentation says for

Pop all back stack states up to the one with the given identifier.
This function is asynchronous — it enqueues the request to pop, but
the action will not be performed until the application returns to its
event loop.

So after mFragmentManager.popBackStack(null, 0); your backstack is not empty until your event is completely processed.

Use popBackStackImmediate() for empty it immediately in the current event itself.