在 android 应用内更改语言时应用状态发生变化
App State Changes when changing language within the android app
我们在 android 应用程序中总共使用了 6 种语言,用户可以从中进行选择。我们正在以编程方式设置语言环境。语言是
英语、印地语、泰米尔语、泰莱古语、卡纳达语、马拉雅拉姆语、马拉地语。
我们使用下面的代码更改应用程序的语言。
locale = new Locale(lang);
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
conf.locale = locale;
getApplicationContext().getResources().updateConfiguration(conf,getBaseContext().getResources().getDisplayMetrics());
当我们更改印地语和英语之间的语言时,应用程序能够从共享首选项中获取数据,但对于其他语言,它 returns 对于相同的查询为 null。请帮忙
更新 1:以下是从共享首选项中检索数据的代码
SharedPreferences sharedPrefs = context.getSharedPreferences(context.getString(R.string.shared_prefs_key), Context.MODE_PRIVATE);
String value = sharedPrefs.getString(key, null);
由于您使用字符串值从 sharedpreferences R.string.shared_prefs_key
获取数据,这就是出现问题的原因,因为密钥会在不同的语言字符串中发生变化。并且由于数据存储在不同的键中,因此它为其他语言键提供 null。
将其更改为如下常量值:
SharedPreferences sharedPrefs = context.getSharedPreferences("<your key>", Context.MODE_PRIVATE);
我们在 android 应用程序中总共使用了 6 种语言,用户可以从中进行选择。我们正在以编程方式设置语言环境。语言是 英语、印地语、泰米尔语、泰莱古语、卡纳达语、马拉雅拉姆语、马拉地语。
我们使用下面的代码更改应用程序的语言。
locale = new Locale(lang);
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
conf.locale = locale;
getApplicationContext().getResources().updateConfiguration(conf,getBaseContext().getResources().getDisplayMetrics());
当我们更改印地语和英语之间的语言时,应用程序能够从共享首选项中获取数据,但对于其他语言,它 returns 对于相同的查询为 null。请帮忙
更新 1:以下是从共享首选项中检索数据的代码
SharedPreferences sharedPrefs = context.getSharedPreferences(context.getString(R.string.shared_prefs_key), Context.MODE_PRIVATE);
String value = sharedPrefs.getString(key, null);
由于您使用字符串值从 sharedpreferences R.string.shared_prefs_key
获取数据,这就是出现问题的原因,因为密钥会在不同的语言字符串中发生变化。并且由于数据存储在不同的键中,因此它为其他语言键提供 null。
将其更改为如下常量值:
SharedPreferences sharedPrefs = context.getSharedPreferences("<your key>", Context.MODE_PRIVATE);