在运行时更改显示语言并重新创建所有活动

Change display language at runtime and recreate all activities

我希望能够在运行时更改 Android 应用程序中的显示语言,而无需重新启动应用程序。为此,我相信可以重新创建所有正在运行的活动。问题是我不知道如何获取对所有未被 ActivityManager.

破坏的活动的引用

在所有活动的 android:configChanges 中添加 "locale" 属性,然后在活动中覆盖 onConfigurationChanged() 并相应地处理语言更改。

更多关于http://developer.android.com/guide/topics/manifest/activity-element.html#config

此方法适用于所有 API 级别的设备,请确保在以编程方式更改语言时重新创建 activity。

1.Use BaseActivity attachBaseContext 设置区域设置语言 extends 所有活动 Activity

open class  BaseAppCompactActivity() : AppCompatActivity() {

    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(LocaleHelper.onAttach(newBase))

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

}
  1. 使用应用程序 attachBaseContextonConfigurationChanged 设置区域设置语言

     public class MyApplication extends Application {
    
       private static MyApplication application;
    
        @Override
        public void onCreate() {
            super.onCreate();
        }
    
        public static MyApplication getApplication() {
            return application;
        }
    
        /**
         * overide to change local sothat language can be chnaged from android device  nogaut and above
         */
        @Override
        protected void attachBaseContext(Context base) {
            super.attachBaseContext(LocaleHelper.INSTANCE.onAttach(base));
        }
    
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
        /*** also handle chnage  language if  device language chnaged **/
    
            super.onConfigurationChanged(newConfig);
    
        }
    
  2. 使用区域设置 Helper 处理语言更改,此方法适用于所有设备

     object LocaleHelper {
    
    
    fun onAttach(context: Context, defaultLanguage: String): Context {
        return setLocale(context, defaultLanguage)
    }
    
    
    
    fun setLocale(context: Context, language: String): Context {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            updateResources(context, language)
        } else updateResourcesLegacy(context, language)
    
    }
    
    
    @TargetApi(Build.VERSION_CODES.N)
    private fun updateResources(context: Context, language: String): Context {
        val locale = Locale(language)
        Locale.setDefault(locale)
    
        val configuration = context.getResources().getConfiguration()
        configuration.setLocale(locale)
        configuration.setLayoutDirection(locale)
    
        return context.createConfigurationContext(configuration)
    }
    
    private fun updateResourcesLegacy(context: Context, language: String): Context {
        val locale = Locale(language)
        Locale.setDefault(locale)
    
        val resources = context.getResources()
    
        val configuration = resources.getConfiguration()
        configuration.locale = locale
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLayoutDirection(locale)
        }
        resources.updateConfiguration(configuration, resources.getDisplayMetrics())
        return context
      }
    }