timothyjc timothyjc - 2 years ago 220
Android Question

AppCompat MODE_NIGHT_AUTO not working

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:


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.

Answer Source

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;

protected void onCreate(Bundle savedInstanceState) {
   mCurrentNightMode = getCurrentNightMode();

protected void onPostResume() {

    if (hasNightModeChanged()) {


private void delayedRecreate() {
    Handler handler = new Handler();
    handler.postDelayed(this::recreate, 1);

private boolean hasNightModeChanged() {
    return mCurrentNightMode != getCurrentNightMode();

private int getCurrentNightMode() {
    return getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download