I want to provide my users an in-app (NOT DEVICE-WIDE) locale (language) change. Thats why I setup the following code which triggers when the user clicks on a specific language:
private void setLocale(Locale locale) {
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
}
So far so good but since now I really don't know how to update/refresh all of my active activities (current activity and all activities in the back stack). Do I have to override onResume() of each activity? Could there be a possibility to generalize that?
I would use an Eventbus library such as this one.
You could also create some sort of Settings, an OnLocaleChangedListener interface, let all Activities (or other classes) listen for changes, something like this:
public class LocaleSettings {
Locale locale;
List<OnLocaleChangedListener> listeners;
void changeLocale(Locale newLocale){
this.locale = newLocale;
for(OnLocaleChangedListener listener : listeners){
listener.localeChanged(newLocale);
}
}
void addListener(){
}
void removeListener(OnLocaleChangedListener toRemove){
}
interface OnLocaleChangedListener{
void localeChanged(Locale locale);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With