Skip to content

Instantly share code, notes, and snippets.

@gnuton
Created June 22, 2013 06:20
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 gnuton/5836079 to your computer and use it in GitHub Desktop.
Save gnuton/5836079 to your computer and use it in GitHub Desktop.
Fuck you Fragments IDs/TAGs! Here is the way to map dynamic fragments in your app!
public class FragmentUtils {
private static Map mMap = new HashMap<String, Fragment>();
public static Fragment getFragment(FragmentManager fm, String className, String tag){
if (tag == null)
tag = className;
Fragment f = (Fragment) mMap.get(tag);
if (f != null)
return f;
try {
//Java reflection is cool!!
f = (Fragment) Class.forName(className).newInstance();
f.setRetainInstance(true);
mMap.put(tag, f);
return f;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment