Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 340
DevAndroid Avatar asked Sep 13 '18 12:09

DevAndroid


People also ask

How to implement day/night mode in your Android app?

How to Implement Day/Night Mode in Your Android App 1 Find out the easiest way to do it yourself. Since Android Pie (9.0) introduced the dark/light (day/night) theme to its users, a lot of the most popular apps have welcomed ... 2 Now, let’s start. ... 3 Moving on to some coding. ... 4 Checking the current system theme. ... 5 More control? ...

What is the DayNight functionality in appcompat?

The DayNight functionality in AppCompat allows your app to easily switch between a dark ⚫ and light ⚪ theme. This has many benefits for your users, from saving power on OLED displays, to increasing usability for people with reduced-vision, and more.

How can I have different themes for day and night modes?

Have a theme with the same name in each file (e.g., AppTheme ), but tailor the theme based upon whatever differences you want have between day and night modes.

How do I change the configuration of an Android activity?

To declare that your activity handles a configuration change, edit the appropriate <activity> element in your manifest file to include the android:configChanges attribute with a value that represents the configuration you want to handle. Possible values are listed in the documentation for the android:configChanges attribute.


3 Answers

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

like image 154
matt.mic Avatar answered Oct 17 '22 11:10

matt.mic


Probably you need to add 'uiMode' parameter to your manifest:

android:configChanges="orientation|screenSize|uiMode|keyboardHidden"
like image 27
user10786506 Avatar answered Oct 17 '22 10:10

user10786506


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 AppCompat v1.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.

like image 22
ElegyD Avatar answered Oct 17 '22 09:10

ElegyD