首页 > 解决方案 > 如何在 SharedPreferences 中保存我的应用程序主题?

问题描述

我创建了一个应用程序,我可以在其中自定义主题并使用导航视图抽屉上的切换按钮将其从浅色更改为深色。但是我只能保存 Switch 按钮的状态,但不能保存 Theme 状态。因此,如果有问题或者我可以对代码做些什么,以便能够保存深色/浅色主题与“切换”按钮检查状态同时进行,请提供帮助。问题是,当我单击切换按钮时一切正常,但是当我关闭应用程序并再次打开它时,切换按钮仍处于选中状态,但主题恢复为默认值(浅色主题)。有人可以帮忙吗?我的代码如下所示:

实用程序类:

public class Utils {
private  static int sTheme;
public final static int THEME_DARK = 1;
public final static  int THEME_LIGHT = 0;
//Set the theme of the Activity and restart the activity
public static void changeToTheme(Activity activity, int theme) {
    sTheme = theme;
    activity.finish();
    activity.startActivity(new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));
    activity.overridePendingTransition(0,0);
}
//Set the theme of the activity according to the configuration
public static void onActivityCreateSetTheme(Activity activity) {
    switch (sTheme) {
        default:
        case THEME_LIGHT:
            activity.setTheme(R.style.AimBet_Light);
            break;
        case THEME_DARK:
            activity.setTheme(R.style.AimBet_Dark);
            break;
    }
}

}

主要活动:

公共类 MainActivity 扩展 AppCompatActivity 实现 NavigationView.OnNavigationItemSelectedListener {

SwitchMaterial themeSwitch;

SharedPreferences myPrefs;
SharedPreferences.Editor myEditor;

private static final String MY_PREFS = "switchPrefs";

private static final String DARK_MODE_SWITCH_STATUS = "switchStatus";

boolean dark_mode_switch_status;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Utils.onActivityCreateSetTheme(this);
    
    setContentView(R.layout.activity_main);

    navigationView.getMenu().findItem(R.id.nav_theme).setActionView(new SwitchMaterial(this));

    themeSwitch = (SwitchMaterial) navigationView.getMenu().findItem(R.id.nav_theme).getActionView();

    myPrefs = getSharedPreferences(MY_PREFS, MODE_PRIVATE);
    myEditor = myPrefs.edit();

    dark_mode_switch_status = myPrefs.getBoolean(DARK_MODE_SWITCH_STATUS, false); //false is the default value

    themeSwitch.setChecked(dark_mode_switch_status);

    themeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (themeSwitch.isChecked()) {
                Utils.changeToTheme(MainActivity.this, Utils.THEME_DARK);
                myEditor.putBoolean(DARK_MODE_SWITCH_STATUS, true);
                myEditor.apply();
            } else {
                Utils.changeToTheme(MainActivity.this, Utils.THEME_LIGHT);
                myEditor.putBoolean(DARK_MODE_SWITCH_STATUS, false);
                myEditor.apply();
            }
        }
    });

}

}

标签: javaandroidsharedpreferencesandroid-dark-theme

解决方案


首先,在切换开关时将数据保存到设备中。

myEditor.putBoolean(DARK_MODE_SWITCH_STATUS, isChecked)
myEditor.apply()

然后,在 onCreate()

if(myPrefs.getBoolean(DARK_MODE_SWITCH_STATUS)){
    Utils.changeToTheme(MainActivity.this, Utils.THEME_DARK);
} else {
    Utils.changeToTheme(MainActivity.this, Utils.THEME_LIGHT);
}

这真的应该工作。让我知道是否还有问题!:)


推荐阅读