0

I'm trying to implement dark mode in my application and faced strange behavior of the MODE_NIGHT_YES|MODE_NIGHT_NO flags.

I use AppCompatDelegate.setDefaultNightMode(...) to set global dark mode. So when I use MODE_NIGHT_FOLLOW_SYSTEM everything is perfect and works as expected but when I use MODE_NIGHT_YES or MODE_NIGHT_NO my application still reacts on System Dark Mode Toggle settings and recreates my activities when it's happening. I expect that if I set MODE_NIGHT_YES or NO system settings should not affect my application in anyway but it does.

Is this something expected or is it a bug? Also I do not handling uiMode config changes by myself, everything is done by using AppCompatDelegate.setDefaultNightMode(...). But it looks like I should do it to prevent unnecessary activity recreation.

Olexii Muraviov
  • 1,456
  • 1
  • 12
  • 36

1 Answers1

0

That behaviour is correct though. Every time you do either:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

Or

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

Your activity must be recreated to apply that.

On the other side, if you use: AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) Your activity will be initialized with the System theme mode preferences ( if BuildCompat.isAtLeastQ())

  • Yes, for FOLLOW_SYSTEM everything is fine, but when I set some strict theme like Light only with flag MODE_NIGHT_NO and than will toggle System Dark Mode settings my app will be recreated no matter what on every system toggle change (by System Toggle I mean os wide toggle in notifications bar). I assume that if my application has 1 theme only so System Settings should not recreate my app on every change but it is. – Olexii Muraviov Oct 18 '19 at 08:20
  • Sorry made a mistake in my comment. Check out the explanation again – Fernando Prieto Moyano Oct 18 '19 at 08:41
  • Yeah, I understand, and this is expected when I set this value, but what is not is after I sat MODE_NIGHT_NO (my activity is recreated and this is ok) and than I toggle dark mode on/off from device notifications menu my activity is also recreated (I mean again after MODE_NIGHT_NO values was already sat). this behaviour is expected with MODE_NIGHT_FOLLOW_SYSTEM flag so app could recreates itself on system toggle change to apply theme but no when u don't want to follow system. and this is exactly my issue, when I'm not following system I don't want my activities ti be recreated on system change. – Olexii Muraviov Oct 18 '19 at 09:06
  • Continue: first recreation when u setting value MODE_NIGHT_NO is fine this is expected but all following recreation on System Dark mode toggle change is not (this toggle is not part of sdk or code it's android 10 system ui button) – Olexii Muraviov Oct 18 '19 at 09:08