3

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?

spyfx
  • 1,311
  • 4
  • 14
  • 24

2 Answers2

2

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);
    }
}
fweigl
  • 21,278
  • 20
  • 114
  • 205
  • 1
    But how would you update the Back Stack activity strings after changing the language? e.g. if you force an activity restart on the Back Stack activities, the current activity will be replaced by the intent. @Ascorbin – spyfx Jan 02 '16 at 18:24
1

If they all extend Activity, you could create a super class that has the locale check in onStart(). Then you could extend your custom Activity and only have to implement your locale check once.

Something like this:

public abstract class LocaleActivity extends Activity {
    @Override
    protected void onStart() {
        setLocale(yourLocale);
    }
}

Then, let's say you have an activity called MainActivity. You could implement it, like so:

public class MainActivity extends LocaleActivity {
    //no need to override onStart, because we inherit it, whenever the user starts this activity in any way, the locale will set.
}
MeetTitan
  • 3,383
  • 1
  • 13
  • 26
  • Inheritance won't work in general - I basically can't extend every `Activity` with my own super activity. e.g. the super class won't help if I need to extend `FragmentActivity`. – spyfx Jan 02 '16 at 08:24
  • Then make `LocaleFragmentActivity` :) – MeetTitan Jan 02 '16 at 08:26
  • I guess it's the wrong approach - there a many other specific `Activity` classes to extend. I created a topic about this a few days ago: http://stackoverflow.com/questions/34517555/abstract-class-to-get-rid-of-redundant-code-in-activities/34518453#34518453 I guess I could do that as well via `ActivityLifecycleCallbacks` somehow. – spyfx Jan 02 '16 at 08:31
  • Follow the path of least resistance. – MeetTitan Jan 02 '16 at 08:37