6

I follow Android N change language programmatically to changed language of my app in android N and above. However, I still have the problem with the application context instance.

In my Application class:

private static Application mInstance;

public static Context getApplication() {
    return mInstance;
}

@Override
public void onCreate() {
    super.onCreate();

    mInstance = this;
}

The language is changed, but Resources get from the Application context is not changed. For example:

MyApplication.getApplication().getResources().getString(stringId);  

With return the wrong language string.

Can I update the application instance in this situation? I stuck to this problem for several hours. Because the MyApplication.getApplication() have used in many places throughout my app. So I can't convert to the Activity context.

Many thanks.

Ngo Van
  • 857
  • 1
  • 21
  • 37
  • Try calling onCreate for Application again when language change is done. `MyApplication.getApplication().onCreate()`. Alternatively you could try restarting the app on language change, that should set the context. – jitinsharma Nov 03 '17 at 04:06
  • did you add your **`MyApplication`** under **`application`** in manifest file – AskNilesh Nov 03 '17 at 04:06

1 Answers1

0

I have the same issue with one of my apps, because I do love my managers and utilities that doesn't require the context being passed every time.

My solution involves two separate contexts, one application context and one locale context. This doesn't solve all issues like inflating with correct locale using the correct style, for this you need to use the activity context. However, if you need to get the correct string or image from the resources based on the current locale, then this solution will work.

public class MainApplication extends Application {
    private static Context applicationContext;
    private static Context localeContext;

    public static Context getAppContext() {
        return applicationContext;
    }

    public static Context getLocaleContext() {
        return localeContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setTheme(R.style.AppTheme);
        applicationContext = getApplicationContext();
        updateLocaleContext();
    }

    public static void updateLocaleContext() {
        localeContext = LocaleHelper.wrapContext(applicationContext);
    }
}

The LocaleHelper.wrapContext should use a similar solution as the accepted answer on Android N change language programmatically and all activites need to implements attachBaseContext. Every time the language changes MainApplication.updateLocaleContext needs to be called. Note: the localeContext do not retain the style set in the onCreate function

Now you can use the MainApplication.getLocaleContext() for resources that depend on correct locale, while using MainApplication.getAppContext() for, e.g., inflating views that do not depend on the locale. Note: you could also place the localeContext in LocaleHelper to reduce the coupling