0

My device system is in English.

In my AndroidManifest.xml, I defined my activity to check configuration changes:

<activity
   ...
   android:configChanges="locale" >

In my Activity, I add the function :

@Override
public void onConfigurationChanged(Configuration newConfig) {

      super.onConfigurationChanged(newConfig);



       Locale.setDefault(newConfig.locale);

       Log.v("*Locale is*", newConfig.locale.toString());

       getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());

}

In my Activity onResume(), I called the above function:

@Override
public void onResume() {
    super.onResume();

     // I explicitely force my app to display in Finnish
     Configuration newConfig = new Configuration();
     newConfig.locale = new Locale("fi");
     onConfigurationChanged(newConfig);
}

(My Activity hosts fragments, Each screen view is a Fragment.)

With above code, I suppose my app will show in Finnish when launched. It works fine on Android 4.1.1.

But when I run my app on Android 2.3.3 device, the following thing happens:

Scenario 1: Launch the app from desktop ==> the app is showing in Finnish, No problem

Scenario 2: Login to my app, ==> then close the app ==> then, launch the app again from desktop ==> the app is showing in English!! Why???

(the log message Log.v("*Locale is*", newConfig.locale.toString()); shows me "fi" always!)

I verified that in Scenario 2, app always show the system default Locale when launch it again from desktop. Why?

I have no idea why in my Scenario 2, my app is showing in system locale English....any one could help?

4

2 回答 2

2

您可以务实地更改语言环境,例如

public void setDefaultLocale(Context context, Locale locale) {
        Locale.setDefault(locale);
        Configuration appConfig = new Configuration();
        appConfig.locale = locale;
        context.getResources().updateConfiguration(appConfig,
                context.getResources().getDisplayMetrics());
        System.out.println("trad" + locale.getLanguage());
    } 

并像这样调用这个方法

setDefaultLocale(getBaseContext(), Locale.FRENCH);

setDefaultLocale(getBaseContext(), Locale.TRADITIONAL_CHINESE);

setDefaultLocale(getBaseContext(), Locale.ITALIAN);

您可以选择要更改的语言并启动当前活动以反映更改。

Intent intent = new Intent(this, YourActivity.class); 开始活动(意图);

嗨,约翰,我从我这边进行了测试以克服您的 Scenario-2,在务实地更改语言环境并转到主屏幕并再次启动我的应用程序后,它仍然是我更改后的语言,即意大利。

我在此链接中发布了代码

请通过这个,让我知道它对你有用。

于 2013-03-11T11:38:11.150 回答
0

它看起来像 Android 2.x 中的一些错误

因为如果您更改语言环境而不是在某些地方,onCreate()但在createMenu()某些地方或内部spinner.setOnItemSelectedListener(),它会起作用。

这是一种解决方法,但在启动您的应用程序并更改其中的语言环境后尝试调用一些事件。

于 2013-03-30T00:55:08.850 回答