The danger of using getSimpleName() as TAG for Fragment

When we want to add a Fragment into a container of an Activity, we’ll normally use a TAG as it’s ID.

getFragmentManager().beginTransaction()
.add(containerResource, fragment, fragmentTAG).commit()

What’s a good TAG name we could use? It’s the class name itself. To simplify thing, maybe just use the simple name of the class, given we knew we never name any of our class the same name ever, even if they are in different packages.

public static final String TAG =         
MyClassFragment.class.getSimpleName();

Cool. When we debug and run, everything is good. Even in released proguard mode, they are fine without issue… until…

The Danger…

One day… we start to see some crashes. A very strange crash happen prior to adding the Fragment to the Activity. The error comes after we load the Fragment

fragment = getFragmentManager().findFragmentByTag(tag);

After some investigation, apparently the fragment loaded is a wrong fragment. How could that be?

Investigation…

After some debug, apparently the tag of my two different fragments is having the same tag name, despite the class name is different…

--

--