[英]Android change theme on result activity
I'm trying to change the theme of my app when I return to an activity from a Settings Activity. 从设置活动返回活动时,我试图更改应用程序的主题。
Change the theme there and then the app theme should change. 在此处更改主题,然后应更改应用程序主题。 Unfortunately it does not do that.
不幸的是,它没有这样做。 When I go back and forth between the Settings and MainActivity nothing happens.
当我在Settings和MainActivity之间来回切换时,什么也没发生。 I'm not finishing the MainActivity but am finishing the Settings activity.
我没有完成MainActivity,但是正在完成Settings活动。
Main -> Settings, change theme (Theme doesn't change) -> Back to Main (Nothing's changed) -> Settings (Still hasn't changed) -> Main (It changed) 主菜单->设置,更改主题(主题不变)->返回主菜单(没有任何更改)->设置(仍未更改)->主菜单(已更改)
I don't know how to change it right away and I am using the recreate() method but it still doesn't work. 我不知道如何立即更改它,我正在使用recreate()方法,但它仍然无法正常工作。
SettingsActivity: 设置活动:
public class GeneralPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
setHasOptionsMenu(true);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
//bindPreferenceSummaryToValue(findPreference("themeChanger"));
//bindPreferenceSummaryToValue(findPreference("example_list"));
SharedPreferences prefs = getSharedPreferences("theming", MODE_PRIVATE);
String themer = prefs.getString("themeID", "0");
val = themer;
Preference.OnPreferenceChangeListener pop = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
String stringValue = o.toString(); //the value set in the strings.xml
String preferenceKey = preference.getKey(); //the key set in the actual xml file
Log.e("Settings " + preference.getKey(), "Clicked " + o);
setThemed();
checkValues();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(
index >= 0
? listPreference.getEntries()[index]
: null);
if(preferenceKey.equalsIgnoreCase("themeChanger")) {
//setThemed();
}
if(!val.equals(stringValue)) {
Log.e("Settings2 " + preference.getKey(), "Clicked2 " + o);
}
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
findPreference("themeChanger").setOnPreferenceChangeListener(pop);
pop.onPreferenceChange(findPreference("themeChanger"),
PreferenceManager
.getDefaultSharedPreferences(findPreference("themeChanger").getContext())
.getString(findPreference("themeChanger").getKey(), ""));
}
public void setThemed() {
SharedPreferences prefs = getSharedPreferences("theming", MODE_PRIVATE);
String themer = prefs.getString("themeID", "0");
setTheme(themer.equals("2") ? R.style.NightTheme1 : R.style.LightTheme);
}
private void checkValues() {
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String themed = sharedPrefs.getString("themeChanger", "0");
SharedPreferences.Editor editor = getSharedPreferences("theming", MODE_PRIVATE).edit();
editor.putString("themeID", themed);
editor.apply();
}
In our app we do it by the following method: 在我们的应用中,我们通过以下方法进行操作:
lastTheme
field lastTheme
字段 lastTheme
contains a value, if so, check if current theme is different from it. lastTheme
包含一个值,如果是,则检查当前主题是否与此值不同。 Sample code: 样例代码:
protected void launchSettings() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
lastTheme = prefs.getInt("theme", -1);
// start settings activity here...
}
@Override
protected void onResume() {
super.onResume();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
int selectedTheme = prefs.getInt("theme", -1);
if ((lastTheme != -1) && (lastTheme != selectedTheme)) {
Log.d(TAG, "the theme was changed");
recreate();
}
lastTheme = -1;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.