Posted on

Why You Should Always Use String Constants For Bundles


android bundle string constants

 Designed by Freepik

Here’s one more tip for Android development from me. Always use string constants for Bundles and for shared preferences. Here’s why

What’s a Bundle?

First of all, a Bundle is a class which can hold bunch of different primitives or parcelable objects

Most common use case for it is passing between activities, you do it implicitly by using wrapper Intent methods

Where putExtra has multiple overloaded methods and under the hood, it has a Bundle just like we used in the first example

Also, it’s used implicitly in Fragment’s factory method newInstance created for you by Android Studio templates

If you hear factory method term for the first time – you probably haven’t read Effective Java by Joshua Bloch. If you haven’t – then buy it right now! I mean it, you should read this book before doing any Android development. Incredible, easy to read and most important – fun book. Despite how much I hate reading books – totally recommend this one. Now with Java 9 updates!

Copy/Paste Keys

First of all, let’s use the most common use case with passing arguments to activities

As you see, I used hardcoded key strings in both cases. I can type pretty fast, but accuracy – not the best. That’s why I copy/paste something important all the time

If you copy/pasted keys to second activity from the first one – that’s at least not as bad as if you attempted to type each key manually. Most keys are more than 4 letters long and consist of actual words where we all make frequent typos

So OK, you copy/pasted keys, there’s no chance of error, right? Correct, not for now. Trust me, with time you’ll be adding, deleting, changing those arguments and there you’ll get more chances of those kinds of situations

Noticed something? Not that easy, right?  There’s a coma in a second activity’s key1. Lint check won’t help you, and it’s really hard to notice that

What you’ll end up with is staring at your phone and not understanding why there’s argument missing in other activity. And since most projects are complex, you’ll be staring at a completely different place in code, rather than misspelled Bundle keys. Mostly at the way you get those values from user input etc

And even if you look at first/second activities keys – you might not even notice some small typo, coma or what so ever. And I’m not making this stuff up right now – it happened to me more than 2 times already!

Why String Constants

Here’s the solution – always use string constants for keys

Now how much there’s room for error? Zero or zero? Exactly!

Even if you’re horrible at spelling – it won’t affect anything

String Resources

It’s just as good idea to extract those string constants to string resources

strings.xml

Just don’t forget to set translatable to false. This will make this string disabled for translations by Android Studio or some other localization software

Since somebody else can be doing translation – he doesn’t know about the context and will just translate this key like other strings

Where it will hurt is in persistent storage like shared preferences. Basically, when a person changes a language on his phone – shared preferences will be gone. It’s very uncommon. but either way, disabling keys for translation saves time for a person doing those translations

Don’t Tell Me What To Do

I know myself, it’s always hard to take an advice from somebody about how to do things. I always hate when somebody tells me how to do things

But trust me, those are very common bugs. People just don’t like talking about their lame moments. I faced this so many times, that it’s just as much an advice for me like it is for you. And you can either learn from my mistakes or from your own. Your choice

 

Ok, hope you found it useful and fun! Don’t forget to subscribe, follow me on Twitter, Facebook, G+ to get notified about more of those cool posts I write weekly


  • Jalle

    I think you just invented hot water. Thanks