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