tags:

views:

50

answers:

2
+1  Q: 

Enum in Activity

I have trouble understanding the following thing: in my localized application I have an Enum in an activity that stores some localized strings (R.string.aString...) which are compared against another localized string.

If while in application I change the locale and I came back and start the Activity that contains the Enum I observe that it's members have are the same as before localization change.

What is the reason for this?

Edit :

class Settings extends Activity 
{

    public enum SettingPreferenceScreen
    {
        Connection (R.string.Connection , xml_resource_1)
        Legend (R,string.Legend ,xml_resource_2)
        .......

    String key;
    int res;

    SettingPreferenceScreen(String key, int res)
    {....}

    public int getResource (String key)
    {
      for(SettingPreferenceScreen p : SettingPreferenceScreen.values())
        if(key.equals(p.key))
           return p.res;

      return -1;
    }

    }

}
+1  A: 

First of all, try avoiding Enums when you develop for android.

Second, my guess is that the Enum get created on the onCreate() method of your Activity and when you open the application for the second time that method is not called. Check the Activity's lifecycle.

Macarse
No..that why I'm surprised too.
Enums are great for readability, and enables us to make cool designs. The "avoid enums" tip on Android developers website is in "Design for performance" category, so you don't have to avoid them when you want to code the elegant 80% of your app.Second, Enum can't be created in some class's method, they are initialized when they are loaded by class loader. What you can do in your Activity is only assign different instances of the Enum object to the Enum reference.
ognian
A: 

R.string does not contain strings, it contains resource ID constants. (Autogenerated int values.) These IDs will be the same regardless of the configuration. The ID constants are used to fetch application resources from a Resources object (or from a Context, which calls through to your Resources). When you call getString or similar, the system will return the localized resource if applicable.

It seems like you're trying to reimplement functionality that Android already provides for you. Can you give us some more details if this is not the case?

adamp