开发者

Enum in Activity

I have trouble understanding the following thing: in my localized a开发者_开发知识库pplication 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;
    }

    }

}


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.


R.string does not contain strings, it contains resource ID constants. (Auto-generated 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 when applicable.

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


Update: It is not necessary to avoid enums in Android anymore... I don't know when it came but the android sdk now includes proguard which optimizes the code at build time and replaces the enums with int constants (cf. proguard.cfg).

Furthermore, the paragraph telling you not to use enums in the dev guide has been removed (http://developer.android.com/guide/practices/design/performance.html)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜