Why FragmentManager's getBackStackEntryCount() return zero?
Asked Answered
K

5

14
private static void changeFragment(Fragment f, boolean init) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.info_content, f,f.getClass().getName());
        if(!init){
            ft.addToBackStack(null);
        }
        ft.commit();
    }

when I want to get the stack cout by call fm.getBackStackEntryCount(), it returns zero?

Kenelm answered 20/12, 2012 at 2:29 Comment(2)
what is value of bool init?Zillion
whatever true or false,it always be zero.Kenelm
Z
29

You have to call a fm.executePendingTransactions() after ft.commit() or before fm.getBackStackEntryCount(). Because the commit() only schedules the transactions for a later pass.

Zillion answered 15/1, 2013 at 16:45 Comment(2)
I know this answer is from 2 years ago but I am having similar issues and calling getSupportFragmentManager().executePendingTransactions(); after commit did not resolve the issue. Can you suggest any other changes?Standice
When executePendingTransactions() is called after the commit(), an exception is thrown. If called before the getBackStackEntryCount(), then count is still zero.Fatness
C
20

I had a similar problem, in my case getFragmentManager().getBackStackEntryCount() was always returning zero.

My problem was I've using support fragments:

Fragment fragment = new MyFragment();
// note getSupportFragmentManager() instead getFragmentManager()
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame, fragment)
.addToBackStack(null)
.commit();

fragmentManager.executePendingTransactions();

and I've checking getFragmentManager() backStackEntryCount, which always returns zero (it's using another fragment manager):

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() > 0 ) {
        getFragmentManager().popBackStack();            
    }
}

instead of getSupportFragmentManager, which returns the correct number:

@Override
public void onBackPressed() {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0 ) {
        getSupportFragmentManager().popBackStack();         
    }
}

Hope it helps!

Clearness answered 17/8, 2015 at 8:23 Comment(1)
Omg, this is such a potential bugs source for all developers. Thanks man.Berneicebernelle
S
6

It might be too late to answer this question. Hope this answer will help someone anyway.

Mostly it depends on where you are actually calling getBackStackEntryCount() method. In my case, I was calling this method after calling super.onBackPressed(). The moment this method was got called, there was no fragment in back stack. That's why I was always receiving 0.

Right way of calling the method in onBackPressed() :

   @Override
public void onBackPressed() {
    try {
        int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();
        Log.d("class", "items in backstack " + backStackEntryCount);
    } catch (Exception e) {
        e.printStackTrace();
    }
    super.onBackPressed();
}
Shrove answered 17/5, 2015 at 10:55 Comment(0)
K
4

Another solution is using FragmentManager.OnBackStackChangedListener

fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            Log.d("test", "backStackEntryCount: " + fm.getBackStackEntryCount());
        }
});
Koeninger answered 28/5, 2015 at 7:30 Comment(0)
S
1

It might be too late to answer this question. Hope this answer will help someone anyway.

You should getBackStackEntryCount() method in onResume().

It will be this:

@Override
protected void onResume() {
    super.onResume();
    Log.i(TAG, "onResume: " + fragmentManager.getBackStackEntryCount());
    for (int entry = 0; entry < fragmentManager.getBackStackEntryCount(); entry++) {
        Log.i(TAG, "Found fragment: " + fragmentManager.getBackStackEntryAt(entry).getId());
    }
}

Good luck!

Supereminent answered 20/7, 2017 at 6:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.