5

Edit: Updated in the bottom

I'm calling this in Application's onCreate: AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)

Then when I'm going to the device's settings (Settings -> Display -> Night mode (switch: on/off)) then I'm resuming my application, the theme is not applied. It doesn't matter if I'm turning ON or OFF the night mode in the device's settings, the theme is not applied.

I also added a breakpoint and I checked that the following is returning me false even if the Dark Mode is ON from device's settings (Note: the app was started with Dark mode OFF).

fun isNightMode(app: Application): Boolean {
     return when(app.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK) {
         Configuration.UI_MODE_NIGHT_NO -> false
         Configuration.UI_MODE_NIGHT_YES -> true
         else -> false
     }
 }

It looks like the application's resource doesn't get updated when I'm changing the theme from device settings.

For debug purpose I override the following function in the Application class:

override fun onConfigurationChanged(newConfig: Configuration?) {
   super.onConfigurationChanged(newConfig)
} 

and it's getting called.

Edit: It looks like this is causing the problem. Having this in the Application class:

override fun attachBaseContext(base: Context) {
   val locale = Locale("de", "DE")

   Locale.setDefault(locale)

   val resources = base.resources
   val configuration = Configuration(resources.configuration)

   configuration.setLocale(locale)
   val baseContext = base.createConfigurationContext(configuration)
        
   super.attachBaseContext(baseContext)
}

If I remove the code above it's working.

Zbarcea Christian
  • 9,367
  • 22
  • 84
  • 137
  • 1
    I am facing the same issue but works after commenting out attachBaseContext(). Were you able to fix the issue with attachBaseContext()? – Prashanth Dec 03 '20 at 00:29
  • @Prashanth - i fixed it by adding the uiMode to the newly created configuration class. `configuration.uiMode = Configuration.UI_MODE_NIGHT_YES` – Zbarcea Christian Dec 03 '20 at 08:55
  • Christan - I need to set uiMode to take the system theme Light/Dark but with configuration.uiMode = Configuration.UI_MODE_NIGHT_YES we will be setting it as always "Dark" theme right? – Prashanth Dec 03 '20 at 09:31
  • Yes, you need to set either light, dark or follow system settings. – Zbarcea Christian Dec 03 '20 at 09:38
  • Resources res = context.getResources(); Configuration config = new Configuration(res.getConfiguration()); config.uiMode = Configuration.UI_MODE_NIGHT_YES; There is no configuration to set system theme right? If I set config.uiMode = Configuration.UI_MODE_NIGHT_YES; it is always displaying in Dark theme. In Application class onCreate() I have this code AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM). Any idea how can I set the configuration so that it displays theme based on system settings? – Prashanth Dec 03 '20 at 09:43
  • There are plenty tutorials for setting light/dark or use system settings tutorial on the web. My problem and yours was because of the `attachBaseContext`, because here we override the current configuration without setting a UI MODE. Search for a tutorial on the web how to change light/dark theme and remove your attachBaseContext function and check if it's working. If it's working, now you have to fix the attachBaseContent function by passing the UI MODE to your newly created Configuration object. – Zbarcea Christian Dec 03 '20 at 10:14
  • 2
    Zbarcea-Thank you, I fixed the issue. I had to just set uiMode config.uiMode = Configuration.UI_MODE_NIGHT_UNDEFINED; Works perfectly fine now. – Prashanth Dec 03 '20 at 10:19

1 Answers1

4

Just to provide a standalone answer here.
Thanks to @Zbarcea Christian and @Prashanth for their comments.

The problem for me was the same: overriding attachBaseContext() method. So, fixed it like this:

override fun attachBaseContext(cxt: Context) {
    val resources = cxt.resources
    val configuration = Configuration(resources.configuration)
    // Required for the day/night theme to work
    configuration.uiMode = Configuration.UI_MODE_NIGHT_UNDEFINED
    val baseContext = cxt.createConfigurationContext(configuration)

    // set locale and so on ...

    super.attachBaseContext(baseContext)
}
Mahozad
  • 18,032
  • 13
  • 118
  • 133