Adam Varhegyi
Adam Varhegyi

Reputation: 9894

Clearing the fragments back stack

I know there are ot of questions about this on stackoverflow, but my problem is unique i think.

Here is my code:

public void emptyBackStackNow() {
    FragmentManager fragmentManager = act.getFragmentManager();


    Log.i("Fragments count on the stack: ", fragmentManager.getBackStackEntryCount() + "");
    for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {

        String fragmentTag = fragmentManager.getBackStackEntryAt(i).getName();
        Log.i("Fragment on the stack: ", fragmentTag);

    }

    Log.i("Fragments count on the stack, right before popping: ", fragmentManager.getBackStackEntryCount() + "");
    for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {

        fragmentManager.popBackStackImmediate();
        Log.i("Fragment popped from stack: ", "just popped: "+i+".");

    }

    Log.i("Fragments count on the stack after popping: ", fragmentManager.getBackStackEntryCount() + "");
    for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {

        String fragmentTag = fragmentManager.getBackStackEntryAt(i).getName();
        Log.i("Fragments still on the stack:", fragmentTag);

    }

}

And the strange log:

2-05 10:30:10.649: I/Fragments count on the stack:(25133): 14
12-05 10:30:10.649: I/Fragment on the stack:(25133): ContactFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): IdopontKeresFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): IdopontjaimFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): LeleteimFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): TeethbrushFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): MessageListFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): IdopontjaimFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): ContactFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): IdopontKeresFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): IdopontjaimFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): SegedanyagokFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): ContactFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): ASZFFragment
12-05 10:30:10.649: I/Fragment on the stack:(25133): MessageListFragment
12-05 10:30:10.649: I/Fragments count on the stack, right before popping:(25133): 14
12-05 10:30:10.729: I/Fragment popped from stack:(25133): just popped: 0.
12-05 10:30:10.739: I/Fragment popped from stack:(25133): just popped: 1.
12-05 10:30:10.739: I/Fragment popped from stack:(25133): just popped: 2.
12-05 10:30:10.749: I/Fragment popped from stack:(25133): just popped: 3.
12-05 10:30:10.749: I/Fragment popped from stack:(25133): just popped: 4.
12-05 10:30:10.779: I/Fragment popped from stack:(25133): just popped: 5.
12-05 10:30:10.789: I/Fragment popped from stack:(25133): just popped: 6.
12-05 10:30:10.789: I/Fragment popped from stack:(25133): just popped: 7.
12-05 10:30:10.789: I/Fragments counton the stack after popping:(25133): 7
12-05 10:30:10.789: I/Fragments still on the stack:(25133): ContactFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): IdopontKeresFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): IdopontjaimFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): LeleteimFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): TeethbrushFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): MessageListFragment
12-05 10:30:10.789: I/Fragments still on the stack:(25133): IdopontjaimFragment

You can clearly see, the second for loop not iterating throu the whole stack.

Simple question: How could this possible?

Upvotes: 1

Views: 407

Answers (1)

amit singh
amit singh

Reputation: 1422

This is because, when you are popping out the fragment in this loop-

    for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) 
    {
        fragmentManager.popBackStackImmediate();
        Log.i("Fragment popped from stack: ", "just popped: "+i+".");

    }

then, "fragmentManager.getBackStackEntryCount()" will return the total count one less than the previous count. To overcome this problem, you have to do it like this-

           int count = fragmentManager.getBackStackEntryCount();
           for (int i = 0; i < count; i++) 
            {
                fragmentManager.popBackStackImmediate();
                Log.i("Fragment popped from stack: ", "just popped: "+i+".");

            }

Upvotes: 1

Related Questions