The danger of using getSimpleName() as TAG for Fragment

Elye
Elye
Aug 18, 2016 · 2 min read
Image for post
Image for post

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

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.

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

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. Here is how it happens.

I have two fragments. After proguard, my fragments name changed to

com.mypackage.FragmentA → com.mypackage.c.a

com.mypackage.FragmentB → com.mypackage.c.a.a

That’s the post proguard full name. How about their short simple name i.e. getSimpleName()?

com.mypackage.FragmentA → a

com.mypackage.FragmentB → a

Wo! Wo! Wo!… The same name. No wonder when I try to find my fragment, it could potentially retrieve a wrong fragment…. when we are really really fortunate to have Proguard give the same simpleName for it.

Lesson Learned

I know the chance might not be high that the getSimpleName() will be the same on post proguard; but if it happens to me, it could happen to anyone. One should use either one of the below perhaps

  • the full name
  • canonical name
  • a constant explicitly defined

Mobile App Development Publication

Sharing Mobile App Development and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store