Why FragmentManager's getBackStackEntryCount()

2019-01-15 07:07发布

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?

5条回答
▲ chillily
2楼-- · 2019-01-15 07:41

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.

Melony?
3楼-- · 2019-01-15 07:48

Another solution is using FragmentManager.OnBackStackChangedListener

fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            Log.d("test", "backStackEntryCount: " + fm.getBackStackEntryCount());
        }
});
查看更多
太酷不给撩
4楼-- · 2019-01-15 07:48

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!

查看更多
老娘就宠你
5楼-- · 2019-01-15 07:53

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();
}
查看更多
一夜七次
6楼-- · 2019-01-15 07:54

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!

查看更多
登录 后发表回答