简体   繁体   中英

Reading locale settings from shared preferences doesn't work for the MainActivity

I am changing Locale settings inside my app with this method:

public void setLanguageSettings(String localeLang){
    Locale locale = new Locale(localeLang);
    saveString("lang", localeLang);
    Locale.setDefault(locale);
    Configuration configSlo = new Configuration();
    configSlo.locale = locale;
    getBaseContext().getResources().updateConfiguration(configSlo, getBaseContext().getResources().getDisplayMetrics());
}

As you can see, the localeLang String variable is then saved to SharedPreferences. This variable is in my case either "sl" or "en" (default is "en" ).

Then I'm calling this (same) method everytime the application starts (every time MainActivity is opened):

String language = getString("lang");
setLanguageSettings(language);

The Locale is changed successfully (I checked with log) but the problem is that the change can't be seen in the MainActivity. If I open any other activity, the language is correct, but the MainActivity remains "en" even tho it is supposed to be "sl" . I'd have to restart MainActivity for it to be "sl" .

I solved this question in a weird way. I just called setContentView again in the change language method:

setContentView(R.Layout.MainLayout);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM