Search code examples
androidconfigurationlocale

android bug or a misuse of Configuration object?


We have a bit of code in the Application object onCreate method that changes the default local in the configuration of the ApplicationContext.

it looks something like:

Locale locale = new Locale(sSavedLocale);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());

Than we start our main activity and from there our other activities and tasks. As long as i don't cause configuration change like screen rotation everything is ok. if i rotate the screen the Locale returns to the device default, even though the application object remains the same.

Digging into android srouce code of ActivityThread and other inner classes i could see the the ApplicationContext itself and the context in the mainThread is changed back to default on configuration change event.

It seems like a bug to me since i i set the configuration in the application level at app start i probably want to maintain the change unless Local configuration change occurred, and even then....

I didn't check for open bugs i do think of applying one. Anyone thinks i'm wrong and this is ok behavior ?


Solution

  • You can add onConfigurationChanged to your Application class.

        @Override
        public void onConfigurationChanged(Configuration newConfig)
        {
            super.onConfigurationChanged(newConfig);
            if (newConfig.locale != null)
            {
                Locale locale = new Locale(sSavedLocale);
                Locale.setDefault(locale);
                Configuration config = new Configuration();
                config.locale = locale;
                getResources().updateConfiguration(config, getResources().getDisplayMetrics());
            }
        }