[英]App State Changes when changing language within the android app
We are using a total of 6 languages inside our android app, which user can choose from. 我们在android应用程序中总共使用6种语言,用户可以选择。 We are setting the locale programmatically. 我们正在以编程方式设置语言环境。 The languages are English, Hindi, Tamil, Telegu, Kannada, Malayalam, Marathi. 语言是英语,印地语,泰米尔语,泰勒古语,卡纳达语,马拉雅拉姆语,马拉地语。
Using the code below we change the app's language. 使用下面的代码,我们可以更改应用的语言。
locale = new Locale(lang);
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
conf.locale = locale;
getApplicationContext().getResources().updateConfiguration(conf,getBaseContext().getResources().getDisplayMetrics());
When we change the language between Hindi and English app is able to get the data from shared preferences, but for the other languages, it returns null for the same query. 当我们在北印度语和英语之间更改语言时,应用程序能够从共享首选项中获取数据,但对于其他语言,对于同一查询,它返回null。 Please help 请帮忙
Update 1 : Following is the code to retrieve data from shared preference 更新1:以下是从共享首选项中检索数据的代码
SharedPreferences sharedPrefs = context.getSharedPreferences(context.getString(R.string.shared_prefs_key), Context.MODE_PRIVATE);
String value = sharedPrefs.getString(key, null);
Since you are using string value to get the data from sharedpreferences R.string.shared_prefs_key
that is why the issues is occurring because the key would be changing in different language string. 由于您使用字符串值从sharedpreferences R.string.shared_prefs_key
中获取数据,因此这就是为什么发生此问题的原因,因为密钥会以不同的语言字符串更改。 And since the data was stored during different key, it gives null for other language keys. 并且由于数据是在不同的键期间存储的,因此对于其他语言键,它会提供空值。
Change that to a constant value like below: 将其更改为恒定值,如下所示:
SharedPreferences sharedPrefs = context.getSharedPreferences("<your key>", Context.MODE_PRIVATE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.