Home > database >  Android app dark theme gone after application killed and restarted
Android app dark theme gone after application killed and restarted

Time:11-13

I am trying to implement light/dark theme in my app. The change of themes works fine if I don't kill the app. But if I do, for example, before killing it I had set it to Dark theme. After restarting the app, every activity and fragment goes back to light theme again.

I implemented shared preferences but still can't seem to figure out what's the issue.

Set theme OK button code:

                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();

                    }
                });


And in very activity or fragment's oncreate method, I do this:


        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);


Application onCreate code:

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);

        }

    }
}

CodePudding user response:

You need to set the theme on Application onCreate using AppCompatDelegate.setDefaultNightMode just as you did with a button click but with saved data. It will change the theme across the application.

You can remove the setTheme calls at the beginning.

Also, you need to remove the if (mPreferences.contains(SP_THEME_KEY)) checks. There may be an issue where the values are not being saved because there was no key to begin with. The Editor automatically manages the creation and use of nonexistent keys.

  • Related