androidkotlinsharedpreferencesandroid-appcompatandroid-darkmode

Dark mode in app is not applying after removing app from recent background


First see this picture below

[white mode 1] https://i.sstatic.net/QKcju.jpg

[white mode 2] https://i.sstatic.net/QKcju.jpg

[Dark mode 1 ] https://i.sstatic.net/QKcju.jpg

[Dark mode 2 ] https://i.sstatic.net/QKcju.jpg

The problem is -> when i am opening dark activity by clicking on drawer layout settings button and after pressing dark button dark mode is applied to whole app, when I am deleting app from recent background and then opening app then white mode is auto applying to the activity home but after opening dark activity again from drawer layout it is auto applying dark mode.

Here is the code i've implemented

    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

Solution

  • You need to call AppCompatDelegate.setDefaultNightMode(mode) before super.onCreate(savedInstanceState).