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?

beiliubei
  • 173
  • 1
  • 1
  • 8

5 Answers5

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.

Arvis
  • 8,273
  • 5
  • 33
  • 46
  • 4
    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? – Rain Man Aug 14 '15 at 19:22
  • When executePendingTransactions() is called after the commit(), an exception is thrown. If called before the getBackStackEntryCount(), then count is still zero. – Ray Hunter Aug 07 '16 at 22:55
19

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!

Neonigma
  • 1,825
  • 17
  • 20
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();
}
minhazur
  • 4,928
  • 3
  • 25
  • 27
4

Another solution is using FragmentManager.OnBackStackChangedListener

fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            Log.d("test", "backStackEntryCount: " + fm.getBackStackEntryCount());
        }
});
gilchris
  • 1,231
  • 17
  • 24
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!

dotrinh PM
  • 903
  • 1
  • 7
  • 19