Issue
In Android app development, it's often recommended to define constant values as XML resources under res/values. Doing so confers many benefits - app localization is easier to accomplish, device configuration-specific values can be used at runtime, and code is generally cleaner, among other things. My question is, are there cases where this should be avoided, and why?
For example, why do
<string name="base_url">http://api.mycompany.com/</string>
rather than simply
public static final String BASE_URL = "http://api.mycompany.com/";
?
Solution
It's probably more straightforward to use a constant when the string won't be localized. Notice that "localized" is not the same as "translated". For example, formatting patterns for currency or date aren't translated into a different language, but you should put them into res/values/ so that your localizers can adjust them for a particular locale.
On the other hand, you may want some human-readable strings to be constants, such as intent actions. For example:
public static final String ACTION_START_DOWNLOAD = "com.example.android.ACTION_START_DOWNLOAD";
If I know that a constant is used among several different peer classes, I put it in a separate peer class (rather like a C header file). Also, I use a contract class to define constants that I use with a content provider. Splitting out the constants like this makes it much easier to change them.
Answered By - Joe Malin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.