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 fragment transaction to back stack
fragTrans.addToBackStack(null);
//commit the transaction
fragTrans.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragTrans.commit();
Later I have some fragments replace the previous fragment, but NOT put on backstack.
It seems for those fragment which have not been put on backstack will always showing on screen, the back button press will have not effect to them.
So, How to find those fragments which are not on backstack in my app, and how to remove them from screen?
Call
findFragmentById()orfindFragmentByTag()on yourFragmentManager.Use
remove()with aFragmentTransaction.