java - 重新创建活动后语言没有改变,需要重新创建活动2次然后它改变了android
问题描述
本地管理器
public class LocalManager {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static Context onAttach(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
return setLocale(context, lang);
}
public static Context onAttach(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
return setLocale(context, lang);
}
public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}
public static Context setLocale(Context context, String language) {
persist(context, language);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, language);
}
return updateResourcesLegacy(context, language);
}
private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}
private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Configuration configuration = context.getResources().getConfiguration();
configuration.setLocale(locale);
configuration.setLayoutDirection(locale);
return context.createConfigurationContext(configuration);
}
@SuppressWarnings("deprecation")
private static Context updateResourcesLegacy(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);
Resources resources = context.getResources();
Configuration 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;
}
}
基础应用
在基本应用程序中,我在 attachBaseContext 和 onConfigurationChanged 中附加 localmanager
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(LocalManager.onAttach(base))
}
override fun onConfigurationChanged(newConfig: Configuration) {
LocalManager.onAttach(applicationContext)
super.onConfigurationChanged(newConfig)
}
活动
private fun changeLanguage(it: String) {
LocalManager.setLocale(applicationContext, it)
val intent = intent
intent.putExtra(CALLINFO, callInfo)
intent.putExtra(CALLUID, callUUID)
if (poName.isNotEmpty()) {
intent.putExtra(PO_NAME, poName)
}
intent.putExtra(SELECTED_LANGUAGE,selectedLanguage)
finish()
startActivity(intent)
}
override fun attachBaseContext(base: Context?) {
super.attachBaseContext(LocalManager.onAttach(base))
}
override fun onConfigurationChanged(newConfig: Configuration) {
LocalManager.onAttach(applicationContext)
super.onConfigurationChanged(newConfig)
}
当用户从微调器中选择语言并在 localmanager 中设置该语言代码并重新创建活动时,调用 changeLanguage() 函数
我还通过 attachBaseContext() 和 onConfigurationChanged() 在此活动中附加了 localmanager
问题是,当用户第一次从微调器中选择语言并调用 changeLanguage() 函数时,然后重新创建活动,但是当用户再次从微调器选择中更改语言时,语言没有改变,然后再次重新创建活动,但这次语言被更改
所以我希望当用户第一次选择微调器并调用 changeLanguage() 函数时,需要更改语言
请建议方法
解决方案
推荐阅读
- php - Laravel 6 PHP 7
- java - java中标记接口的意义何在?为什么没有它们我们就不能继续?
- javascript - 为多个父标签创建子标签时出现问题
- code-coverage - 如何在 Visual Studio for Mac COMMUNITY 8.3.4 中运行显示代码覆盖率的测试
- c# - 如何在 ASP.Net MVC C# - Re-Factorization 中获取方法的返回值(DRY 原则)
- node.js - Mongoose 中的自动索引是什么?
- attributes - 为什么 Dymola/Modelica 中的属性 min、max 不起作用?
- rust - 将 Rust 属性应用于多行
- ios - 致命错误:在展开可选值 UIPageViewController 时意外发现 nil
- html - 如何使用蚂蚁设计在 ReactJS 中将半圆形蒙版应用于图像并在图像内添加按钮?