'Android DayNight Theme with configChanges
I'm creating an app where I use the theme DayNight of the Android support libraries.
This is the code in themes.xml
<style name="ActivityTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
I use configChanges in the manifest to manage rotations.
android:configChanges="keyboardHidden|orientation|screenSize"
In the onCreate on my AppcompatActivity I have added:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
The problem is that when I use setDefaultNightMode, android:configChanges stops working, and recreate de activity in every rotation.
Any kind of help will be welcome
Solution 1:[1]
Seems to be a bug and will be fixed in AppCompat v.1.1.0 according to this issue.
Until its fixed I would add the uiMode flag to android:configChanges like mentioned here
Solution 2:[2]
Probably you need to add 'uiMode' parameter to your manifest:
android:configChanges="orientation|screenSize|uiMode|keyboardHidden"
Solution 3:[3]
This issue has been fixed with AppCompat v1.1.0-alpha05.
See here: https://developer.android.com/preview/features/darktheme#changing_themes_in-app
Note: Starting with
AppCompatv1.1.0-alpha05, setDefaultNightMode() does not automatically recreate any started activities.
Update your appcompat dependency to the latest version, currently 1.1.0-rc01.
implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
Don't forget to remove the uiMode flag from android:configChanges in your manifest if you added it.
Solution 4:[4]
Starting with AppCompat v1.1.0, setDefaultNightMode() automatically recreates any started activities.
See https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#changing_themes_in-app
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | matt.mic |
| Solution 2 | Jaswant Singh |
| Solution 3 | ElegyD |
| Solution 4 | Tal Vaknin |
