Created
June 22, 2013 06:20
-
-
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!
This file contains hidden or 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
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