Created
April 27, 2014 14:44
-
-
Save djodjoni/11347343 to your computer and use it in GitHub Desktop.
Remove multiple fragments from the same container without knowledge of their tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//--> 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