0

先看下面这张图

【白色模式1】https://i.stack.imgur.com/QKcju.jpg

【白色模式2】https://i.stack.imgur.com/QKcju.jpg

[黑暗模式1] https://i.stack.imgur.com/QKcju.jpg

[黑暗模式2] https://i.stack.imgur.com/QKcju.jpg

问题是->当我通过单击抽屉布局设置按钮打开黑暗活动并按下黑暗按钮后将黑暗模式应用于整个应用程序,当我从最近的背景中删除应用程序然后打开应用程序时,白色模式将自动应用于活动回家,但从抽屉布局再次打开暗活动后,它会自动应用暗模式。

这是我实现的代码

    const val FIRST_START = "FirstStart"
    const val NIGHT_MODE = "NightMode"
    const val PREF = "AppSettingsPrefs"
    class language_settings : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            loadLocate() // call LoadLocate
            setContentView(R.layout.activity_language_settings)
    
            //save preferences specific to an app, like save the dark mode again when we again 
    open the app
            val appSettingsPrefs: SharedPreferences = getSharedPreferences(PREF, 0)
            val isNightModeOn: Boolean = appSettingsPrefs.getBoolean(NIGHT_MODE, false)
            val isFirstStart: Boolean = appSettingsPrefs.getBoolean(FIRST_START,false)
            //Create a new Editor for these preferences  through which you can make 
    modifications to
            // the data in the preferences and atomically commit those changes back to the                 
    SharedPreferences object
            val editor: SharedPreferences.Editor = appSettingsPrefs.edit()
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && isFirstStart ){
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
            }
            else{
                when {
                    isNightModeOn -> {
                        
    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
                    }
                    else -> {
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
                    }
                }
            }
            btnSwitch.setOnClickListener {
                if (isNightModeOn) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
                    editor.putBoolean(FIRST_START, false)
                    editor.putBoolean(NIGHT_MODE, false)
                    editor.apply()
                    recreate() //recreate activity to make changes visible, onPause, onStop, 
    onDestroy, endAllActiveAnimators, onStart, onResume
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
                    editor.putBoolean(FIRST_START, false)
                    editor.putBoolean(NIGHT_MODE, true) 
                    editor.apply()
                    recreate() //recreate activity to make changes visible, onPause, onStop, 
    onDestroy, endAllActiveAnimators, onStart, onResume
    
                }
            }
please don't mind my english ;P
4

1 回答 1

0

你需要先AppCompatDelegate.setDefaultNightMode(mode)打电话super.onCreate(savedInstanceState)

于 2021-09-03T08:07:14.200 回答