androidandroid-appcompatandroid-resourcesandroid-night-mode

Activity is recreated when screen rotation changes if-and-only-if Night Mode is ON


I have this Activity (subclass of AppCompatActivity):

<activity
    android:name=".ui.settings.SettingsActivity"
    android:configChanges="keyboardHidden|smallestScreenSize|orientation|screenSize|screenLayout"
    android:launchMode="singleTask"
    android:parentActivityName=".ui.main.MainActivity" />

Now, when I rotate the phone while in normal (i. e. "Day") mode I get a call to onConfigurationChanged but the Activity is not re-created.

However, when the app is set to Night Mode (by calling AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES) in my Application before the Activity is created) and I then rotate the phone, I first get a call to onConfigurationChanged but then the Activity is also re-created, even though the uiMode has not changed at all! This goes away when I add uiMode to the configChanges that should be ignored in my AndroidManifest.xml, but it seems weird that I need to do this.

From looking through the source it seems that:

So, what's going on here and what could I do to get more consistent behavior? (Re-create (or not) the Activity after a screen rotation, regardless of whether the app is in Night Mode or not.)


Solution

  • Seems to be a bug and will be fixed in AppCompat v1.1.0 according to this issue.

    Starting with AppCompat v1.1.0-alpha03 you no longer need the uiMode-flag in the configChanges as a workaround