After the screen rotation, the language of my application will be changed

后端 未结 3 1544
伪装坚强ぢ
伪装坚强ぢ 2021-02-13 17:58

I have created a bilingual (having two languages) android application. I have inserted my resource strings in two files:

For Persian language (default)
values/st         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-13 18:39

    You can create custom ContextWrapper

    public class mContextWrapper extends ContextWrapper {
        public mContextWrapper(Context base){
            super(base);
        }
    
        @SuppressWarnings("deprecation")
        public static ContextWrapper wrap(Context context, String language) {
            Configuration config = context.getResources().getConfiguration();
            Locale sysLocale = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                sysLocale = getSystemLocale(config);
            } else {
                sysLocale = getSystemLocaleLegacy(config);
            }
            if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
                Locale locale = new Locale(language);
                Locale.setDefault(locale);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    setSystemLocale(config, locale);
                } else {
                    setSystemLocaleLegacy(config, locale);
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    context = context.createConfigurationContext(config);
                } else {
                    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
                }
            }
            return new mContextWrapper(context);
        }
    
        @SuppressWarnings("deprecation")
        public static Locale getSystemLocaleLegacy(Configuration config){
            return config.locale;
        }
    
        @TargetApi(Build.VERSION_CODES.N)
        public static Locale getSystemLocale(Configuration config){
            return config.getLocales().get(0);
        }
    
        @SuppressWarnings("deprecation")
        public static void setSystemLocaleLegacy(Configuration config, Locale locale){
            config.locale = locale;
        }
    
        @TargetApi(Build.VERSION_CODES.N)
        public static void setSystemLocale(Configuration config, Locale locale){
            config.setLocale(locale);
        }
    }
    

    and in your activity attachBaseContext attach your custom ContextWrapper

     @Override
        protected void attachBaseContext(Context newBase) {
            globalClass = (YouAppClass)newBase.getApplicationContext();
            //try geting the lang you have setted inside your YouAppClass class
            // or you can use shared preferences for it 
            //pref = PreferenceManager.getDefaultSharedPreferences(newBase)
            lang=globalClass .getLang();
            super.attachBaseContext(mContextWrapper.wrap(newBase,lang));
        }
    

提交回复
热议问题