应用主题后Android保存应用程序状态
问题描述:
我创建了一个应用程序,可以通过单击按钮在2个主题(黑白)之间切换。我已经在清单中将默认主题设置为白色。应用主题后Android保存应用程序状态
因此,每次关闭并重新启动应用程序时,主题状态都不会保存并应用白色主题。
任何人都可以给我一些想法或代码,如果可能的话,关于如何保存应用程序的状态,不同的方法来做到这一点?
谢谢。
答
有一个布尔触发器,你检查SharedPreferences
。如果布尔值为true,则将应用程序设置为白色。如果为假,黑色。每当用户更改他/她想要的主题时,请将该布尔值保存在SharedPreferences
中。
代码示例:
在的onCreate():
SharedPreferences mPrefs = getSharedPreferences("THEME", 0);
boolean theme_boolean = mPrefs.getBoolean("theme_boolean", true);
if (theme_boolean) {
// Set theme to white
} else {
// Set theme to black
}
在按钮的onClick():
if (theme_boolean) {
// Set theme to black
theme_boolean = false;
} else {
// Set theme to white
theme_boolean = true;
}
SharedPreferences mPrefs = getSharedPreferences("THEME", 0);
SharedPreferences.Editor mEditor = mPrefs.edit();
mEditor.putBoolean("theme_boolean", theme_boolean).commit();
http://*.com/questions/151777/ save-activity-state-in-android – 2012-07-31 13:26:44