AppCompatDelegate.MODE_NIGHT_AUTO is not updating my existing activity and I'm not sure why.
I dynamically allow the user to change night mode. If the user changes the mode to auto I set the default night mode then recreate the activity:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
recreate();
If I change to MODE_NIGHT_YES or MODE_NIGHT_NO, it works as expected. If I change to MODE_NIGHT_AUTO, it goes to the correct dark/light theme, but then it fails to update the activity after the transition from day to night. It kind of sucks to test this because I have to wait for sunrise/sunset (EDIT: apparently I can manually change the time on the device rather than having to wait...so long as the location permission is not used).
Do I have to do a manual check for the night mode flag in onresume and manually update resources for existing activities, or am I doing something wrong? If I rotate the device and the activity is recreated after sunset then the dark theme is correctly picked up, but before rotation it will still be showing the light theme.
Support lib 23.4.0, Android version 6.0.
In case anyone else wants to know what I did to solve this (not sure if it is the right way to do it though):
private int mCurrentNightMode;
@Override
protected void onCreate(Bundle savedInstanceState) {
mCurrentNightMode = getCurrentNightMode();
}
@Override
protected void onPostResume() {
super.onPostResume();
if (hasNightModeChanged()) {
delayedRecreate();
}
}
private void delayedRecreate() {
Handler handler = new Handler();
handler.postDelayed(this::recreate, 1);
}
private boolean hasNightModeChanged() {
getDelegate().applyDayNight();
return mCurrentNightMode != getCurrentNightMode();
}
private int getCurrentNightMode() {
return getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With