FragmentManager popBackStack does not work - Bug?

11,715

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.

Share:
11,715
Nickolaus
Author by

Nickolaus

Bachelor of Information and Communication Technology (BICT) Webdeveloper shopware AG

Updated on July 22, 2022

Comments

  • Nickolaus
    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
      SohailAziz over 11 years
      look at this question ;
    • Nickolaus
      Nickolaus over 11 years
      the 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
      Nickolaus over 11 years
      additionally if you are refering to .popBackStack() without arguments, this has the same effects and doesn't work either
    • Matt
      Matt over 11 years
      However, you may try mFragmentManager.popBackStackImmediate(null, 0); although you're probably better off calling mFragmentManager.popBackStackImmediate(mainTag, mFragmentManager.POP_BACK_STACK_INCLUSIVE) and ditch the loop
  • andrea.rinaldi
    andrea.rinaldi about 10 years
    That's right. And the two answers below explain how to manage @Nickolaus's issue.
  • anthony
    anthony almost 8 years
    You save my day bro ! Thank you very much !