Why zero fragment in backstack

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 did not put to backstack 

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 ???

+6
source share
2 answers

Try executing PendingTransactions () in front of the log to verify that this has occurred. Like this:

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

Hope this helps ...

+3
source

I am not sure of the answer ... It may be necessary to wait for the fragment to be added? Try to count this with

  new Handler().postDelayed(new Runnable() { @Override public void run() { Log.v("nr of fragment in back stack", fragMgr.getBackStackEntryCount()+""); } },50); 
0
source

Source: https://habr.com/ru/post/913731/


All Articles