Delete fragments that do not fit into the stack

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 snippet that replaces the above snippet 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 fragment transaction to back stack
fragTrans.addToBackStack(null);

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

Later I have fragments that replace the previous fragment, but DO NOT fit in the backstack .

It seems that those fragments that were not placed in the backstack will always be displayed on the screen, pressing the "Back" button will not have an effect for them.

, , backstack , , ?

+3
1

, backstack

findFragmentById() findFragmentByTag() FragmentManager.

?

remove() FragmentTransaction.

+1

All Articles