Android 应用程序终止并重新启动后,应用程序深色主题消失了

Android app dark theme gone after application killed and restarted

我正在尝试在我的应用中实现 light/dark 主题。如果我不终止应用程序,主题的更改就可以正常工作。但是,如果我这样做,例如,在杀死它之前我已经将它设置为深色主题。重新启动应用程序后,每个 activity 和片段都会再次回到浅色主题。

我实现了共享首选项,但似乎仍然无法弄清楚问题所在。

设置主题确定按钮代码:

                bottomSheetView.findViewById(R.id.settingsGeneral_changeTheme_btnOK).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                   
                        switch (tempTheme) {

                            case 1:
                                theme = 1; //update global value

                                //update theme in shared pref
                                if (mPreferences.contains(SP_THEME_KEY)) {

                                    SharedPreferences.Editor spEditor = mPreferences.edit();
                                    spEditor.putInt(SP_THEME_KEY, theme);
                                    spEditor.apply();

                                }

                                //set theme

                                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);

                                break;
                            case 2:
                                theme = 2; //update global value

                                //update theme in shared pref
                                if (mPreferences.contains(SP_THEME_KEY)) {

                                    SharedPreferences.Editor spEditor = mPreferences.edit();
                                    spEditor.putInt(SP_THEME_KEY, theme);
                                    spEditor.apply();

                                }

                                //set theme
                                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

                                break;

                        }

                        bottomSheetDialog.dismiss();

                    }
                });


在非常 activity 或片段的 oncreate 方法中,我这样做:


        mPreferences = getSharedPreferences(spFileName, MODE_PRIVATE); //get sp file

        if (mPreferences.contains(SP_THEME_KEY)) { //if got this key

            theme = mPreferences.getInt(SP_THEME_KEY, 2);

            switch(theme){

                case 1: //dark
                    setTheme(R.style.darkTheme);
                    break;
                case 2: //light
                    setTheme(R.style.appTheme);
                    break;
            }

        } else { //if don't have this key (app first launch)

            theme = 2; //by default its light mode

            SharedPreferences.Editor spEditor = mPreferences.edit();
            spEditor.putInt(SP_THEME_KEY, theme);
            spEditor.apply();

            setTheme(R.style.appTheme);

        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_general_settings);


应用程序创建代码:

public class MigotoApplication extends Application {

    private final String SP_THEME_KEY = "sp_theme_key";

    private SharedPreferences mPreferences;
    private String spFileName = "settingsSpFile";

    private int theme;

    @Override
    public void onCreate() {

        super.onCreate();

        if (mPreferences.contains(SP_THEME_KEY)) { //if got this key

            theme = mPreferences.getInt(SP_THEME_KEY, 2);

            switch(theme){

                case 1: //dark
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                    break;
                case 2: //light
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                    break;
            }

        } else { //if don't have this key (app first launch)

            theme = 2; //by default its light mode

            SharedPreferences.Editor spEditor = mPreferences.edit();
            spEditor.putInt(SP_THEME_KEY, theme);
            spEditor.apply();

            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

        }

    }
}

您需要使用 AppCompatDelegate.setDefaultNightMode 在应用程序 onCreate 上设置主题,就像单击按钮但保存数据一样。它将更改整个应用程序的主题。

您可以删除开头的 setTheme 调用。

此外,您需要删除 if (mPreferences.contains(SP_THEME_KEY)) 检查。可能存在值未被保存的问题,因为没有开始的键。编辑器自动管理不存在的密钥的创建和使用。