Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android locale randomly changes back to default

Tags:

android

locale

i have one activity and in onCreate i load language from preferences and set a locale like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String lang = PreferenceManager.getDefaultSharedPreferences(this).getString("locale", "en");
    Locale newLocale = new Locale(lang);
    Locale.setDefault(newLocale);

    Configuration config = new Configuration();
    config.locale = newLocale;

    final Resources res = getResources();
    res.updateConfiguration(config, res.getDisplayMetrics());
}

i also override onConfigurationChanged

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    String lang = PreferenceManager.getDefaultSharedPreferences(this).getString("locale", "en");
    Locale newLocale = new Locale(lang);
    Locale.setDefault(newLocale);

    Configuration config = new Configuration();
    config.locale = newLocale;

    final Resources res = getResources();
    res.updateConfiguration(config, res.getDisplayMetrics());
}

i have a ViewPager with FragmentPagerAdapter and three tabs on android 2.3.7. the problem is, that sometimes when switching the tabs the app's locale gets reverted back to english, so the UI has mixed languages from now on. i always create new fragments for the tabs in adapter's getItem() method. if i rotate the device the locale is correct again.

i also tried putting android:configChanges="locale" in manifest and not overriding onConfigurationChanged(), but the result is the same.

the worst thing is, that it is not 100% reproducible, it happens only some times and i also have reports from users about this. once the application starts and it do not change the locale after switching few tabs, then it will correctly until exit.

like image 365
shelll Avatar asked Apr 13 '12 10:04

shelll


1 Answers

After some time I figured out a way. The only solution that works for me is always setting the locale in main Acitivity's onCreate(), in each Fragment's onCreateView(), in onReceive() of every BroadcastReceiver() (if it loads some resource) and at the start of every Service. For this I made a static method setLocale() in my main and only activity. If you have more activities it could be placed in a custom Application class.

One last thing. Even this some times does not work if the app is started directly from IDE for debugging. Do not worry, it works (at least for me) if it is run normally on the device.

public static void setLocale(Context context) {
    final String lang = PreferenceManager.getDefaultSharedPreferences(context).getString("locale", "en");
    final Locale newLocale = new Locale(lang);
    Locale.setDefault(newLocale);
    final Configuration config = new Configuration();
    config.locale = newLocale;

    final Resources res = context.getResources();
    res.updateConfiguration(config, res.getDisplayMetrics());
}
like image 94
shelll Avatar answered Nov 12 '22 04:11

shelll