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?
You have to call a
fm.executePendingTransactions()
afterft.commit()
or beforefm.getBackStackEntryCount()
. Because the commit() only schedules the transactions for a later pass.Another solution is using FragmentManager.OnBackStackChangedListener
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:
Good luck!
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() :
I had a similar problem, in my case getFragmentManager().getBackStackEntryCount() was always returning zero.
My problem was I've using support fragments:
and I've checking getFragmentManager() backStackEntryCount, which always returns zero (it's using another fragment manager):
instead of getSupportFragmentManager, which returns the correct number:
Hope it helps!