0

I just want to know if the night mode in Android needs sqlite? or there's another way to save the mode in it when the user close the app and reopen it again it will save the mode pls help

If there's another way please share it with me Thanks

Sairaj Sawant
  • 1,842
  • 1
  • 12
  • 16

4 Answers4

1

No, sqlite is not needed.

Local application settings such as night mode preference are typically stored in SharedPreferences.

laalto
  • 150,114
  • 66
  • 286
  • 303
1

You can just save a variable in the SharedPreferences of the app for the preferred user settings and set theme on launch. Go through the documentation to know more .SharedPreferences

  • Rule of thumb - provide a link rather than the "Go through the documentation". Have a happy contributing! – RevakoOA May 28 '20 at 14:16
1

Please, check Shared Preferences. This is key-value storage and it's exactly suited for such needs.

As general practice - create some SharedPrefManager and move all logic with shared preferences there. Like in this answer.

RevakoOA
  • 601
  • 1
  • 9
  • 20
1

Write this code on the first page that runs

 SharedPreferences pref = getApplicationContext().getSharedPreferences("nightMode", 0);
    if(pref.getBoolean("mode",false)){
        // code change ui to night
    }

and Write this code on the night mode switch

    SharedPreferences pref = getApplicationContext().getSharedPreferences("nightMode", 0);

    pref.edit().putBoolean("mode",true).apply();

Just as easily

Javad Dehban
  • 1,282
  • 3
  • 11
  • 24