简体   繁体   English

暗/亮模式重新创建应用程序后,SwitchCompats 检查不正确

[英]SwitchCompats are checked incorrectly after Dark/Light Mode recreates the app

I have four switches in my BottomSheetDialogFragment .我的BottomSheetDialogFragment中有四个开关。 When I make DarkMode 'On', the app is recreated.当我将 DarkMode 设置为“开启”时,会重新创建应用程序。 The weird thing is that other switches became 'On' even if those were 'Off' before.奇怪的是,即使之前是“关闭”,其他开关也变成了“打开”。 (The value in SharedPref is 'false' but the Switch shows 'on') (SharedPref 中的值为 'false' 但 Switch 显示为 'on')

I use this code to make switches on/off when dialog is created:创建对话框时,我使用此代码打开/关闭开关:

binding.autoPlaySwith.setOnCheckedChangeListener(null)
binding.autoPlaySwith.isChecked = getHawkBoolean(AUTO_PLAY_VIDEO) //read it from sharedPref
binding.autoPlaySwith.setOnCheckedChangeListener(this)

//same code for other switches

And this code for switches checkedChange event(The problem occurs in DarkMode checked on/off):这段代码用于切换checkedChange事件(问题发生在DarkMode检查开/关):

override fun onCheckedChanged(view: CompoundButton, isChecked: Boolean) {

        if (view.isPressed) {

          when (view.id) {

                binding.autoPlaySwith.id -> {
                    saveHawkBoolean(AUTO_PLAY_VIDEO, isChecked)
                }

                binding.themeSwith.id-> {
                    saveHawkBoolean(DARK_MODE, isChecked)
                    try {
                        if (isChecked) {
                          
                            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
                        } else {
                          
                            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
                        }
                    } catch (ex: Exception) {
                        Log.d(TAG , ex.localizedMessage?:"exception occurred ")
                    }
                }
       }
     }
  }

my Controls before recreate the app via DarkMode switch:在通过 DarkMode 开关重新创建应用程序之前我的控件: 在此处输入图像描述

and the image after that:以及之后的图像:

在此处输入图像描述

The code that reads values from sharedPref and sets switches was in the onViewCreated method.从 sharedPref 读取值并设置开关的代码位于onViewCreated方法中。 After I put that code in onResume method, my problem solved.在我将该代码放入onResume方法后,我的问题就解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Android 应用程序更改为浅色/深色主题后,背景 Recyclerview 显示不正确 - Background Recyclerview shows incorrectly after changing to Light/ Dark theme from Android App 切换到浅色模式时应用程序崩溃 - App crashes when changing into light to dark mode 更改为浅色模式时,React Native App 崩溃 - React native App crashes when changing into light to dark mode 我的 React Native 应用程序在光照模式下滞后,但在黑暗模式下超快 - My React Native app is lagging in the light mode, but is superfast in the dark 深色工具栏上的灯光操作模式 - Light action mode on dark toolbar 如何根据一天中的时间在暗模式和亮模式之间切换应用程序? - How to change app between dark mode and light mode depending on the time of day? 带有Light App主题的深色工具栏 - Dark Toolbar with Light App Theme 动态更改为暗/亮模式时闪烁 - Flash when changing to dark/light mode dynamically 不保存亮(白天)和暗(夜)模式 - light (day) and dark (night) mode are not saved 暗模式:useColorScheme() 在 Android 上总是返回光 - Dark Mode: useColorScheme() always returns light on Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM