I am working on globalization of Android app. I have to provide options to choose different locales from within the app. I am using the following code in my activity (HomeActivity) where I provide option to change the locale
Configuration config = new Configuration();
config.locale = selectedLocale; // set accordingly
// eg. if Hindi then selectedLocale = new Locale("hi");
Locale.setDefault(selectedLocale); // has no effect
Resources res = getApplicationContext().getResources();
res.updateConfiguration(config, res.getDisplayMetrics());
This works fine as long as there are no configuration changes like screen rotation where the locale defaults to the android system level locale rather than the locale set by the code.
Locale.setDefault(selectedLocale);
One solution I can think of is to persist the user selected locale using SharedPreferences and in each activity's onCreate() method have the locale set to the persisted locale as the onCreate() gets called again and again for every configuration change. Is there any better way to do this so that I don't have to do it in every activity.
Basically what I want is that - Once I change/set to some locale in my HomeActivity I want all the activities within my app to use that locale itself irrespective of any configuration changes....unless and until it is changed to other locale from the app's HomeActivity that provides options to change locale.