I've read many posts about reloading the Activity. I understand it is not the way to go. However, my PreferenceActivity class loads its view via an xml file. In that xml file all names and descriptions reference the the string.xml file. One of these preference options is to change the language. I use an onPreferenceChangeListener definition to catch and set the new Locale right away. I want behaviour similar to the OS where after you change the language its instantly reflected.
To avoid manually defining each preference option, is there not any way at the end of this method that I can force the xml file to be loaded again and thus all the strings to be grabbed from the new locale?
Thanks for your help, here's what I have so far, I cut out the locale change to save space:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
Preference langPref = (Preference) findPreference("languagePreference");
langPref.setOnPreferenceChangeListener( new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
/* CHANGE LOCALE */
开发者_运维问答 ...
/* FORCE XML TO BE RELOADED */
HOW?
return false;
}
});
}
I'm not saying this is a good answer but to possibly help others, I've been able to make a temporary solution to what I want:
I've put my change listener creation inside a method called setupListener.
/* FORCE XML TO BE RELOADED */
/* remove existing preferences */
PreferenceScreen prefScreen = getPreferenceScreen();
prefScreen.removeAll();
/* add preference screen */
MyPrefClass.this.addPreferencesFromResource(R.xml.preferences);
/* re-add listener */
Preference langPref = (Preference) findPreference("languageLocale");
MyPrefClass.this.setupListener( langPref );
The state of the preference isn't switching properly for me yet but I'm going to live with it for now.
Updated full code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
/* add language listener */
Preference langPref = (Preference) findPreference("langLocale");
this.setupListener( langPref );
}
protected void setupListener( Preference preference ) {
preference.setOnPreferenceChangeListener( new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
/* set master config */
Locale locale2 = new Locale( newValue.toString() );
Locale.setDefault(locale2);
Configuration config2 = new Configuration();
config2.locale = locale2;
/* add preference screen */
getBaseContext().getResources().updateConfiguration(config2, getBaseContext().getResources().getDisplayMetrics());
/* remove existing preferences */
PreferenceScreen prefScreen = getPreferenceScreen();
prefScreen.removeAll();
LusilKanjiDrawPreferences.this.addPreferencesFromResource(R.xml.preferences);
setTitle(R.string.app_name_title);
/* re-add listener */
Preference langPref = (Preference) findPreference("langLocale");
LusilKanjiDrawPreferences.this.setupListener( langPref );
return true;
}
});
}
Warning: some of the methods are deprecated now. Will have to look into possible changes myself.
精彩评论