Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh all active activities (current and back stack) after changing locale programmatically

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?

like image 884
spyfx Avatar asked May 07 '26 02:05

spyfx


1 Answers

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);
    }
}
like image 128
fweigl Avatar answered May 08 '26 15:05

fweigl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!