skip to Main Content

I implement dark mode with SwitchPreference in the second activity and in the first activity just a button take me to the second activity (Setting) the problem is when the app destroyed and i launched it again the dark mode not implemented until the second activity launched.

    //First Activity
    
    public class MainActivity extends AppCompatActivity {
    
        Button button;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
 SharedPreferences sharedPreferences =
                PreferenceManager.getDefaultSharedPreferences( this/* Activity context */);
        Boolean theme = sharedPreferences.getBoolean("darkMode", false);
                
 button=findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent2=new Intent(MainActivity.this,Settings.class);
                    startActivity(intent2);
                }
            });
    
    
    
        }
    
    }

i have some idea that i should use sharedpreferences but i don’t know how can i set it

//Second Activity

    public class MySettingsFragment extends PreferenceFragmentCompat {
    
        @Override
        public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
            setPreferencesFromResource(R.xml.preferences,rootKey);
            SwitchPreference switchPreference=findPreference("darkMode");
    
            if (switchPreference.isChecked()){
    
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
    
            }else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
    
            }
    
    
        }
    }

enter image description here

2

Answers


  1. Well as per your code , you are just one step away from achieving result

    you just need to use below code in every activity’s onCreate method

    if (isDarkMode){ // isDarkMode is boolean which we can get from preference
        
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        
                }else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        
                }
        ```
    
    Login or Signup to reply.
  2. Overview:-

    You can change dark mode or light mode via this function.

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.HERE_YOUR_MODE);
    

    You can access the mode like this:

    AppCompatDelegate.MODE_NIGHT_YES // for dark mode
    AppCompatDelegate.MODE_NIGHT_NO // for light mode
    AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM // for set as your devices theme
    AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM // when you are in battery saver
    

    Implementation:-

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        Boolean theme = sharedPreferences.getBoolean("darkMode", false);
        if (theme){
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); // implement here.
        }else{
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); // implement here.
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search