为什么区域设置被覆写的preferenceActivity时方向的变化? [英] Why locale settings are overriden on PreferenceActivity when orientation changes?
问题描述
我有一个应用程序,用户可以改变它的语言。
I have an app where the user can change its language.
一切都在我的MainActivity.onCreate做工精细,与眼前这个code():
Everything is working fine, with just this code on my MainActivity.onCreate():
String lang = PreferenceManager.getDefaultSharedPreferences(this).getString("languagePref", "default");
Configuration config = getResources().getConfiguration();
if( lang.equals("default") ) lang = Locale.getDefault().getLanguage();
config.locale = new Locale(lang);
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
当我重新启动应用程序或浏览通过各种活动,它仍然在正确的语言。
When I restart the app or navigate through activities it's still in the right language.
唯一的问题是preferenceActivity屏幕上。当方向变化时,preferenceActivity标题(和只有它)更改默认的设备语言。
The only problem is on the PreferenceActivity screen. When the orientation changes, the PreferenceActivity title (and only it) changes to the default device language.
在preFS仍在核对正确,如果我回去(关闭preferenceActivity)的应用程序仍然在正确的语言,但直到我重新启动应用程序的preferenceActivity留错了。
The prefs are still checked correctly, if I go back (closing the PreferenceActivity) the app is still on the right language, but the PreferenceActivity stays wrong until I restart the app.
我试图迫使code以上的preferenceActivity.onCreate()和altough调试似乎确定,prefenceActivity标题保持错了。
I tried forcing the code above on the PreferenceActivity.onCreate() and altough debugging seems OK, the PrefenceActivity Title stays wrong.
下面是我的prefenceActivity code:
Here's my PrefenceActivity code:
public class PreferencesActivity extends PreferenceActivity {
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
此行为不会对任何其他活动发生的:/
This behavior doesn't happen on any other Activity :/
锁定屏幕方向是不是一种选择。
Locking the screen orientation is not an option.
有什么想法?
感谢。
推荐答案
OK,这个固定的我。
OK, this fixed it for me.
@Override
protected void onSaveInstanceState(Bundle outState) {
String lang = PreferenceManager.getDefaultSharedPreferences(this).getString("languagePref", "default");
Configuration config = getResources().getConfiguration();
if( lang.equals("default") ) lang = Locale.getDefault().getLanguage();
config.locale = new Locale(lang);
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
super.onSaveInstanceState(outState);
}
这篇关于为什么区域设置被覆写的preferenceActivity时方向的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!