Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why zero fragment in backstack

My simple layout only has a fragment placeholder:

<FrameLayout
   android:id="@+id/fragment_placeholder"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
  />

I firstly add a 1st fragment to this placeholder:

fragmentTransaction.add(R.id.fragment_placeholder, firstFragment, "first"); //I did not put to backstack

I have a 2nd fragment, which replace the above fragment and put it to back stack:

FragmentManager fragMgr = getSupportFragmentManager();
FragmentTransaction fragTrans = fragMgr.beginTransaction();

//initialize an fragment instance
Fragment secondFragment = initSecondFragment(); 

//replace with the fragment 
fragTrans.replace(R.id.fragment_placeholder, secondFragment, "second");

//Add transaction to back stack
fragTrans.addToBackStack(null);

//commit the transaction
fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragTrans.commit();  

//The following log returns me 0 when counting the number of fragments in back stack, why?  
Log.v("nr of fragment in back stack", fragMgr.getBackStackEntryCount()+"");

But I end up with 0 fragment in back stack, why???

like image 896
Leem.fin Avatar asked Apr 20 '12 13:04

Leem.fin


1 Answers

Try executePendingTransactions() before the Log to ensure that the commit it happens. Like this:

fragMgr.executePendingTransactions();
Log.v("nr of fragment in back stack", fragMgr.getBackStackEntryCount()+"");

Hope it helps...

like image 151
FrankenBel Avatar answered Oct 17 '22 21:10

FrankenBel