I'm trying to adopt best practice for managing light and dark mode in my app, based on guidance here and here.
Based on this, I am using a theme inherited from DayNight
:
In Manifest:
<application
android:theme="@style/Theme.MyApp"
</application>
In themes.xml:
<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorButtonNormal">@color/colorButtonNormal</item>
</style>
This has the desired effect of opening up an Activity in a light or dark theme, based on the device's system setting.
But I want to give the user the option of overriding the mode, e.g. to be always dark (even if the system is in light mode).
But when the system is in light mode, I'm finding that that my Activity initially opens up with a light theme (flash of white), before I get the chance to switch over to the dark theme. I'm switching as soon as I possibly can in the Activity lifecycle:
@Override
protected void onCreate(Bundle savedInstanceState) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
super.onCreate(savedInstanceState);
// ...
But even then, I'm getting an annoying "flash of light" before the Activity switches over to dark.
How do I avoid the flash of light?