FragmentManager popBackStack does not work - Bug?
Solution 1
I certainly would not assume that popBackStack()
has an immediate effect, which your loop appears to do.
Solution 2
You are probably looking for popBackStackImmediate()
. This immediately executes a back stack pop.
Solution 3
you can also use .executePendingTransactions();
after popBackStack();
Solution 4
What documentation says for
popBackStack()
:
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.
Nickolaus
Bachelor of Information and Communication Technology (BICT) Webdeveloper shopware AG
Updated on July 22, 2022Comments
-
Nickolaus almost 2 years
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.
-
SohailAziz over 11 yearslook at this question ;
-
Nickolaus over 11 yearsthe code is inside my onOptionsItemSelected method, and as I said it was working before. I didn't use the function for about 3 weeks or so and now it didn't seem to work anymore
-
Nickolaus over 11 yearsadditionally if you are refering to .popBackStack() without arguments, this has the same effects and doesn't work either
-
Matt over 11 yearsHowever, you may try
mFragmentManager.popBackStackImmediate(null, 0);
although you're probably better off callingmFragmentManager.popBackStackImmediate(mainTag, mFragmentManager.POP_BACK_STACK_INCLUSIVE)
and ditch the loop
-
-
andrea.rinaldi about 10 yearsThat's right. And the two answers below explain how to manage @Nickolaus's issue.
-
anthony almost 8 yearsYou save my day bro ! Thank you very much !