2012-07-31 89 views
0

我创建了一个应用程序,可以通过单击按钮在2个主题(黑白)之间切换。我已经在清单中将默认主题设置为白色。应用主题后Android保存应用程序状态

因此,每次关闭并重新启动应用程序时,主题状态都不会保存并应用白色主题。

任何人都可以给我一些想法或代码,如果可能的话,关于如何保存应用程序的状态,不同的方法来做到这一点?

谢谢。

+0

http://stackoverflow.com/questions/151777/ save-activity-state-in-android – 2012-07-31 13:26:44

回答

2

有一个布尔触发器,你检查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(); 
+0

谢谢....我在哪里将它保存在活动中..例如,我是否将它保存在onPause()或任何其他方法? – crtn 2012-07-31 13:26:16

+0

我为你添加了一个代码示例:)。让我知道这是否有效/如果你有问题。 – Mxyk 2012-07-31 13:28:30

+0

谢谢。有效 :) – crtn 2012-08-01 11:02:47