Admob reset app locale to system default

前端 未结 1 1197
后悔当初
后悔当初 2021-01-21 14:47

I set a local in my launcher activity, after a second or two the locale settings resets to the device\'s system locale settings.

After several hours of digging, I found

相关标签:
1条回答
  • 2021-01-21 15:40

    The issue was caused by using the deprecated updateConfiguration on API's newer than 17, the following code solves the issue (within activity), and sets a persisting locale settings.

    public void attachBaseContext(Context base) {
        Resources resources = base.getResources();
        Locale locale = new Locale("ru");
        Locale.setDefault(locale);
        Configuration config = resources.getConfiguration();
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(locale);
            base = base.createConfigurationContext(config);
        }
        else {
            config.locale = locale;
            resources.updateConfiguration(config, resources.getDisplayMetrics());
        }
    
        super.attachBaseContext(base);
    }
    
    0 讨论(0)
提交回复
热议问题