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.