Skip to content

Instantly share code, notes, and snippets.

@djodjoni
Created April 27, 2014 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djodjoni/11347343 to your computer and use it in GitHub Desktop.
Save djodjoni/11347343 to your computer and use it in GitHub Desktop.
Remove multiple fragments from the same container without knowledge of their tags
//--> REMOVE fragments if any in containerId
//FragmentTransaction.replace does not replace all the fragments in the container but only one thus we need to remove them all one by one
Fragment currFrag = getFragmentManager().findFragmentById(containerId);
while(currFrag!=null) {
try {
getFragmentManager().beginTransaction().remove(currFrag).commit();
// fragment will not be removed instantly so we need to wait for the next one, otherwise too many commits buildup in the heap causing OutOfMemory
android.app.Fragment nextFrag = getFragmentManager().findFragmentById(containerId);
while (nextFrag != null && nextFrag==currFrag) {
nextFrag = getFragmentManager().findFragmentById(containerId);
}
currFrag = nextFrag;
} catch(Exception ex){
//frag manager exception timing issue
//ex.printStackTrace();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment