I have an activity that uses bottom navigation for fragments and an options menu to choose the theme. The problem is when I change the theme the fragment changes as well to the first one, not the one that I am curently on.
This is the code where I change the theme:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.light_mode -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
sharedPrefs = getSharedPreferences("AppPrefs", Context.MODE_PRIVATE)
val editor = sharedPrefs.edit()
editor.putBoolean(THEME_PREF_KEY, false)
editor.apply()
true
}
R.id.dark_mode -> {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
sharedPrefs = getSharedPreferences("AppPrefs", Context.MODE_PRIVATE)
val editor = sharedPrefs.edit()
editor.putBoolean(THEME_PREF_KEY, true)
editor.apply()
true
}
R.id.log_out -> {
logout()
true
}
else -> super.onOptionsItemSelected(item)
}
}
New contributor
Alex is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.