2

Since MIUI 11/12 Use Its Own Custom Implementation of Dark Mode, using below code results in ClassCastException and Activity Life Cycle Exception, as DefaultNightMode and LocalNightMode is always Unspecified i.e. -100 instead of 0 or 1.

If anyone has built a workaround for Xiaomi Devices specially for MIUI 11/12. Please Help Me Out On this.

Code Used:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
this.recreate();

Error Caused:

2020-10-25 07:04:43.626 9167-9167/com.xxxx.xxxxxx E/ActivityInjector: get life cycle exception
    java.lang.ClassCastException: android.os.BinderProxy cannot be cast to android.app.servertransaction.ClientTransaction
Ayush
  • 1,226
  • 1
  • 8
  • 18
  • Have you had a look at this? https://developer.android.com/guide/topics/ui/look-and-feel/darktheme It says recreating the activity is unnecessary. Depending on the Android version underneath MIUI this might be the issue. – ymindstorm Oct 28 '20 at 08:09
  • Thanks for replying. I have looked onto that, The problem is not with Android, its the skin of MIUI that is on top. The app works completely fine on a Stock Android (same api). – Ayush Oct 29 '20 at 03:06
  • I think the exception is coming from the function `Activity.recreate()`, It seems to give this error on Xiaomi devices. – El Nuru Oct 14 '21 at 15:59

2 Answers2

3

I solved it myself. After fiddling around with it for a week, I did it..

Here is how:

-- Instead of setting android:forceDarkAllowed="false" for each and every Activity.

-- Just goto values.xml and place this in your AppTheme

        <item name="android:forceDarkAllowed">false</item>

It will ask you to create a valuesv29.xml. Create it, and remove the forceDarkAllowed="false" from all activities.

Welcome. :)

Ayush
  • 1,226
  • 1
  • 8
  • 18
1

Also in jetpack compose just add the line to the theme.xml

<item name="android:forceDarkAllowed" tools:targetApi="q">false</item>

With this target api worked fine for me