Rajat Sharma
Rajat Sharma

Reputation: 522

Proguard and getSimpleName()

I had a simple check to ensure that the same fragment doesn't get added twice to my stack, suppose on a menu item click.

public void addFragment(Fragment fragmentToBeAdded){
  Fragment topFragment = getCurrentTopFragment();
  //getCurrentTopFragment returns top visible fragment using findFragmentById
    if(!fragmentToBeAdded.getClass().getSimpleName()
     .equals(topFragment.getClass().getSimpleName()){
       //add fragmentToBeAdded
    }
}

Now, of course the first question is whether this approach is incorrect? Also, how it led to an insidious bug is when proguard changed to different classes' "simple names" to the same letter. The logic went for a toss and that too in the release build. Which luckily, wasn't released.

If at all the basic approach is correct, the alternatives in hindsight were to either use getName() instead of getSimpleName() or use an instanceOf check. Please let me know as to what all can be corrected here.

Upvotes: 1

Views: 1495

Answers (1)

Rajat Sharma
Rajat Sharma

Reputation: 522

I think it was simply a wrong way to do things. There is a reason fully qualified class name is preferred, it's unique. Moreover I should have gone with an instanceOf check which probably is better than a string literal.

Upvotes: 1

Related Questions