Issue
The dark mode switch stays positive but dark mode doesn't enable after I close and reboot the app. I'm not sure how to get my application to boot in dark mode if the switch to enable it is turned on. Here is my code:
class Settings : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.settingsssssssss)
val themeSwitch = findViewById<Switch>(R.id.switch1)
val sharedPreferences = getSharedPreferences("save", MODE_PRIVATE)
themeSwitch.setChecked(sharedPreferences.getBoolean("value", false))
themeSwitch.setOnClickListener(View.OnClickListener {
if (themeSwitch.isChecked()) {
val editor = getSharedPreferences("save", MODE_PRIVATE).edit()
editor.putBoolean("value", true)
editor.apply()
themeSwitch.setChecked(true)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
} else {
val editor = getSharedPreferences("save", MODE_PRIVATE).edit()
editor.putBoolean("value", false)
editor.apply()
themeSwitch.setChecked(false)
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
})
}
}
I used "getSharedPreferences" to save the state of the switch so that when I toggle it and close the activity (or app), it stays set how I wanted it and that works well for the switch, but not for the theme. I tried to make the switch's state set a variable's value to 1 or 0 and carried it over into the main activity. Based off that I used a 'if' statement in the main activity for it to set the theme based of the numeric value but that either crashes or only locks the theme to one of the two. Some help or advice would be appreciated as I'm pretty new and clueless.
Solution
You're only calling setDefaultNightMode
inside the click listener - so the Activity
doesn't theme itself when it starts. You need to call it as soon as possible, so in this case:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val sharedPreferences = getSharedPreferences("save", MODE_PRIVATE)
val isNightMode = sharedPreferences.getBoolean("value", false)
// I'd put this in a function somewhere personally, since you use it a few times!
AppCompatDelegate.setDefaultNightMode(
if (isNightMode) AppCompatDelegate.MODE_NIGHT_YES
else AppCompatDelegate.MODE_NIGHT_NO
)
// now you can set up your layout, switch, listeners etc
setContentView(R.layout.settingsssssssss)
...
That way if your Activity
needs to recreate with a different theme, it does so as fast as possible. Also, as far as I'm aware you need to do this for every Activity
in your app, just in case the app gets destroyed in the background. If you go back to the app, whichever Activity
you were in gets recreated, and acts like the "first" Activity
- so it needs to be able to set the night mode for the whole app. Some people create a custom Application
class for this (officially not generally recommended anymore), so you can set the night mode for the whole app in one place.
Also really it shouldn't just be a toggle switch, but that's up to you! There are a few system options besides just "fixed night" and "fixed day" - there's an old (but seems current) dev post here about it. But if you only want to offer those two options, that's fine too
Answered By - cactustictacs
Answer Checked By - Timothy Miller (JavaFixing Admin)