I have a code that switches themes. But if I restart the application, the standard theme is set. Help me how to make sure that the theme is saved, which was selected last time.
class MainActivity : AppCompatActivity(), KodeinAware, SharedPreferences.OnSharedPreferenceChangeListener {
override val kodein by closestKodein()
private val fusedLocationProviderClient: FusedLocationProviderClient by instance()
private lateinit var binding: ActivityMainBinding
private val locationCallBack = object : LocationCallback() {
override fun onLocationResult(p0: LocationResult) {
super.onLocationResult(p0)
}
}
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestWindowFeature(Window.FEATURE_NO_TITLE)
//setContentView(R.layout.activity_main)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
PreferenceManager.getDefaultSharedPreferences(this)
.registerOnSharedPreferenceChangeListener(this)
navController = Navigation.findNavController(this, R.id.nav_host_fragment)
bottom_nav.setupWithNavController(navController)
NavigationUI.setupActionBarWithNavController(this,navController)
if (hasLocationPermission()) {
bindLocationManager()
}
else {
requestLocationPermission()
}
//AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
if (key == "dark_mode"){
val prefs = sharedPreferences?.getString(key, "1")
when(prefs?.toInt()){
1->{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
2->{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
3->{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_TIME)
}
4->{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)
}
}
}
}
}
This my ListPreference
<ListPreference
android:key="dark_mode"
android:title="Темы"
android:defaultValue="1"
app:useSimpleSummaryProvider="true"
android:entries="@array/dark_mode_entries"
android:entryValues="@array/dark_mode_entries_values"/>
Your problem is you only apply the theme in onSharedPreferenceChanged()
, which will only be called at the moment when the setting is modified. You should also do this in onCreate
so the setting is applied every time the Activity opens. Since you're doing it in two places, you should break it out into a separate function.
I also cleaned your code a bit. Call this function inside onCreate()
, and also call it inside onSharedPreferenceChanged()
instead of the code you currently have in that function.
private fun applyDarkModeSetting() {
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val settingValue = sharedPreferences.getString("dark_mode", null)?.toIntOrNull() ?: 1
val mode = when (settingValue) {
1 -> AppCompatDelegate.MODE_NIGHT_YES
2 -> AppCompatDelegate.MODE_NIGHT_NO
3 -> AppCompatDelegate.MODE_NIGHT_AUTO_TIME
else -> AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
}
AppCompatDelegate.setDefaultNightMode(mode)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.