In my simple layout, there is only a fragment:
<FrameLayout android:id="@+id/fragment_placeholder" android:layout_width="fill_parent" android:layout_height="fill_parent" />
First add the 1st snippet to this placeholder:
fragmentTransaction.add(R.id.fragment_placeholder, firstFragment, "first");
I have a 2nd fragment that replaces the above fragment and pushes it back onto the 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 in the end I get fragment 0 in the background stack, why ???
source share