Fragments: Remove all fragments in a view
None of the other answers were really working for me. Here's what I did:
List<Fragment> al = getSupportFragmentManager().getFragments();
if (al == null) {
// code that handles no existing fragments
return;
}
for (Fragment frag : al)
{
// To save any of the fragments, add this check.
// A tag can be added as a third parameter to the fragment when you commit it
if (frag == null || frag.getTag().equals("<tag-name>")) {
continue;
}
getSupportFragmentManager().beginTransaction().remove(frag).commit();
}
or, if you're forced to use it (but not recommended):
.commitAllowingStateLoss();
Also, if you're removing all fragments from the view multiple times, you might consider checking if the current frag is null or isDetached()
or isRemoving()
or you might get NullPointerExceptions
.
Update: The documentation for getSupportFragmentManger().getFragments()
is apparently hidden now, but still works just fine in my code. Here's the screenshot of the documentation:
Having said that, since it is hidden, they no longer want this method used, so see my update below.
Update 8-4-15: If you're not using the support library for fragments, there is unfortunately no getFragments()
available, but there are still a couple, more inconvenient, options.
- Give each
fragment
atag
orid
upon creation, and iterate through them to process eachfragment
as desired. - Create a listener using
onAttachListener
so each time a newfragment
is attached to theactivity
, you can store thatfragment
, and then iterate through that data structure to process eachfragment
as desired.
When not using the getSupportFragmentManager()
, to process a transaction you will need to use getFragmentManager()
instead.
The typical mechanism is to use FragmentManager.findFragmentByTag()
. You use this and add tags to your fragments (or the alternative for id's). This way you can determine what fragments are currently being managed. Then, once you have a handle to a present fragment (findFragmentByTag returns non-null), you can use FragmentManager.beginTransaction()
to start a FragmentTransaction and remove / add the necessary fragments. Working in this way will allow you to avoid the 're-adding' process for the fragment you want to keep.
What I'd probably do is have code like so: (warning psuedo code)
Fragment pane1 = FragmentManager.findFragmentByTag("myFragmentPane1");
Fragment pane2 = FragmentManager.findFragmentByTag("myFragmentPane2");
setupScreen(pane1, pane2);
You should also consider sub-classes of your class instead of having 'everything in one class'. You have a fairly obvious case of Martin Fowler's Replace Conditional with Subclass. Otherwise, I fear this is going to be incredibly hard to manager when you add another screen.
If you use android.support.v4.app.Fragment you can do this:
List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
}
}