FragmentManager popBackStack not working - error?

I use this code to clear my back to my main view:

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

I am sure that this code worked before, but now the backstack count does not change, and the Fragment is not deleted, which causes an exception from memory, because the while loop continues to work.

Does anyone know if something is wrong with him, or if there is an error in the latest version of the SDK tools. I do not know what causes the problem.

+6
source share
4 answers

Of course, I did not expect popBackStack() have an immediate effect, which your loop apparently does.

+5
source

You are popBackStackImmediate() looking for popBackStackImmediate() . This immediately pops up the back stack.

+10
source

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

+7
source

What documentation says popBackStack() :

Put all previous states of the stack to one with the specified identifier. This function is asynchronous - it issues a request for pop, but the action will not be performed until the application returns to its event loop.

So, after mFragmentManager.popBackStack(null, 0); your back stack is not empty until your event has been fully processed.

Use popBackStackImmediate() to remove it immediately in the current event itself.

+2
source

All Articles