Android error or misuse of configuration object?

We have some code in the Object method of the onCreate object, which changes the local default in the ApplicationContext configuration.

It looks something like this:

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

How we will begin our main activity, and from there our other actions and tasks. Until I quit due to a configuration change, as well as to rotate the screen, everything is fine. if I rotate the screen, Locale returns to the device by default, even if the application object remains the same.

Digging out the android srouce ActivityThread code and other internal classes, I could see the ApplicationContext itself, and the context in mainThread returns to default by the configuration change event.

It seems to me that this is a mistake, because I set the configuration at the application level when the application starts. Perhaps I want to save the changes if there was no change in the local configuration, and even then ....

I did not check open errors, I really think about their application. Does anyone think I'm wrong and is this normal?

+1
source share
1 answer

You can add onConfigurationChanged to the 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()); } } 
+4
source

Source: https://habr.com/ru/post/1469194/


All Articles