[英]How to Refresh App with onConfigChanges without loosing instance
protected void onResume() {
super.onResume();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String lang = settings.getString("lang_list", "");
// Log.d("Lang",lang);
if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
recreate();
Locale locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
if (AppController.getInstance().isPreferenceChanged()) {
setupViewPager(viewPager);
adapter.notifyDataSetChanged();
}
}
I want to change the app language without recreate the app.我想在不重新创建应用程序的情况下更改应用程序语言。 I mean I want to do it when user select any language from the setting then it Should change the Setting Activity language without using
recreate();
我的意思是当用户从设置中选择任何语言时我想这样做,然后它应该更改设置活动语言而不使用
recreate();
, because when I use recreate();
,因为当我使用
recreate();
it blinks the App once.它使应用程序闪烁一次。
So I am not using recreate();
所以我没有使用
recreate();
. . Instead I wrote below code in Setting Activity in
AndroidManifest.xml
相反,我在
AndroidManifest.xml
中的设置活动中编写了以下代码
<activity
android:name=".activity.SettingsActivity"
android:label="@string/title_activity_settings"
android:configChanges="orientation|keyboardHidden|screenSize|locale|layoutDirection"/>
As in below screenshot you can see that I have selected "Hindi" for the language but it is not updating the Activity to Hindi.在下面的屏幕截图中,您可以看到我为语言选择了“印地语”,但它没有将活动更新为印地语。 I mean "Select Country", "Select Your Language" and "Select Categories" should be display in Hindi instead of English.
我的意思是“选择国家”、“选择您的语言”和“选择类别”应该用印地语而不是英语显示。 I have wrote String in both languages.
我已经用两种语言编写了 String 。
Can Anybody know How to change it when Change the language ?任何人都可以在更改语言时知道如何更改它吗? OR Why onConfigChanges is not working for locale as it is working for Orientation.
或 为什么 onConfigChanges 不适用于语言环境,因为它适用于 Orientation。
Thank you !谢谢!
In your MainActivity call sharedPrefrences on onStart callback insted of calling in onCreate在您的 MainActivity 调用 sharedPrefrences onStart 回调 insted 调用 onCreate
Example:-例子:-
protected void onStart() {
SharedPreferences sharedPreferences = this.getSharedPreferences("selectedLanguage", Context.MODE_PRIVATE);
String pine = sharedPreferences.getString("language", DEFAULT);
String languageToLoad = pine;
Locale locale = new Locale(languageToLoad);//Set Selected Locale
Locale.setDefault(locale);//set new locale as default
Configuration config = new Configuration();//get Configuration
config.locale = locale;//set config locale as selected locale
this.getResources().updateConfiguration(config, this.getResources().getDisplayMetrics());
invalidateOptionsMenu(); //This changes language in OptionsMenu too
setTitle(R.string.app_name); //calling app name according to language selected by user
super.onStart();
}
You can use a singleton class that will handle the language change without restarting the activity,您可以使用单例类来处理语言更改而无需重新启动活动,
public class MyApplication extends Application
{
private Locale locale = null;
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (locale != null)
{
newConfig.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
@Override
public void onCreate()
{
super.onCreate();
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
Configuration config = getBaseContext().getResources().getConfiguration();
String lang = settings.getString(getString(R.string.pref_locale), "");
if (! "".equals(lang) && ! config.locale.getLanguage().equals(lang))
{
locale = new Locale(lang);
Locale.setDefault(locale);
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.