'Trying to save Night Mode State using Fragment

I am trying to enable Dark Night Mode in my App which is working though but when I restart the app it goes to again Light Mode basically I want to save Dark Night Mode State so when on restart app remain in Dark Mode once it is set to Dark mode, not in Light mode. My relevant code is given below:

     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view= inflater.inflate(R.layout.fragment_settings, container, false);
            switchCompat= view.findViewById(R.id.switchCompat);
            sharedPreferences = getActivity().getSharedPreferences("night",0);
            Boolean booleanValue= sharedPreferences.getBoolean("night_mode",true);
            if(booleanValue){
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                switchCompat.setChecked(true);
    
            }
            switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                        switchCompat.setChecked(true);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putBoolean("night_mode",true);
                        editor.commit();
                    }else{
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                        switchCompat.setChecked(false);
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putBoolean("night_mode",false);
                        editor.commit();
    
                    }
                }
            });
return view;

I also tried this to activity but it is not saving the state of Dark Mode.



Solution 1:[1]

  1. Create a class named Preferences Manager

  2. Paste this code in the class

?

public final SharedPreferences sharedPreferences;

public PreferenceManager(Context context){
    sharedPreferences=context.getSharedPreferences( "PREFS",Context.MODE_PRIVATE );
}
public void putBoolean(String key,Boolean value){
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean( key,value );
    editor.apply();
}
public Boolean getBoolean (String key,Boolean defaultValue){
    return sharedPreferences.getBoolean( key,defaultValue );
}

public void clear(){
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.clear();
    editor.apply();
}
  1. Then in the fragment make an object named preferences manager like this.

    PreferencesManager preferencesManager = new PreferencesManager(getActivity());

  2. Then add value to it like this

if night mode is set to on.

preferencesManager.putBoolean("NightMode",true);

if night mode is off

preferencesManager.putBoolean("NightMode",false);

and then later to check if night mode if on or off, use this code

if (preferencesManager.getBoolean("NightMode")){
    //night mode is on ,do some magic
}else {
    //night mode is off ,do some magic
}

Edit


Alternatively you can also use my library here to store the data. To store and retrieve, see the README.md file

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1