0

I'm new to android programming, and in the app I'm coding there are parts that require get data from shared preferences, which is set in a preference activity. Now, the preference activity is coded mostly in XML, including the pref keys, and the app get this data in Java code. So far, so good. The problem comes with the typos, let's say I write "Settings1" in XML, and "settings1" in java, it's likely that I'll burn every idea debugging but I won't see that. To avoid that I save the strings in a java class, and a XML resources file with the same strings. But still the same problem.

<resources>
   <string name="SETTINGS1">Settings1</string>
</resources>

class Keys {
   public static final String SETTINGS1 = "Settings1";
}

<SwitchPreference
    android:key="@string/SETTINGS1" />

if(sharedPref.getBoolean(Keys.SETTINGS1, true)){ doSomething(); }

But I wanted to write the key value just once, so I came up with two possible solutions. First one:

<resources>
   <string name="SETTINGS1">Settings1</string>
</resources>

class Keys {
   public static final String SETTINGS1 = resources.getString(R.string.SETTINGS1);
}

<SwitchPreference
    android:key="@string/SETTINGS1" />

if(sharedPref.getBoolean(Keys.SETTINGS1, true)){ doSomething(); }

Or:

<resources>
   <string name="SETTINGS1">Settings1</string>
</resources>

<SwitchPreference
    android:key="@string/SETTINGS1" />

if(sharedPref.getBoolean(getString(R.string.SETTINGS1, true)){ doSomething(); }

Which one is better?? I also don't want to introduce too much overhead to the app, so if none of them is good then I won't use them.

2
  • I'm not an expert, but they don't seem extremely different to me. Perhaps you should go with the one you find more readable? Also, a plus side to using Strings.xml is that the string can be referenced from any file, not just that current one as you have it, it seems. Commented Mar 20, 2015 at 16:05
  • The class Keys is available to every class of the app, that isn't something important here Commented Mar 20, 2015 at 16:31

1 Answer 1

1

2nd option is better (and there is minimal overhead doing such a lookup)

Option 1 flawed because it requires access to resources in outer class. Want if you want to read that value from another fragment or activity?

Sign up to request clarification or add additional context in comments.

3 Comments

In both cases strings are accessible application wide. I thought second is better because being the strings in the keys class static and final they'll be initiated at compile time just like the XML strings, but without calling methods to get the strings.
Yes but you need a context in order to lookup a string resource from within your code.
Yes, it's in the class, I didn't wrote it here for simplicity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.