MIUI 11/12主题切换导致LifeCycleException、ClassCastException

MIUI 11/12 Theme Switch Results in LifeCycleException, ClassCastException

自 MIUI 11/12 使用其自己的深色模式自定义实现以来,使用以下代码会导致 ClassCastException 和 Activity 生命周期异常,因为 DefaultNightMode 和 LocalNightMode 始终未指定,即 -100 而不是 0 或 1。

如果有人专门为 MIUI 11/12 构建了小米设备的解决方法。请帮我解决这个问题。

使用的代码:

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

导致的错误:

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

我自己解决了。折腾了一个星期,终于搞定了..

方法如下:

-- 而不是为每个 Activity.

设置 android:forceDarkAllowed="false"

-- 只需转到 values.xml 并将其放入您的 AppTheme

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

它会要求你创建一个valuesv29.xml。创建它,并从所有活动中删除 forceDarkAllowed="false"。

欢迎光临。 :)

同样在 jetpack compose 中,只需将行添加到 theme.xml

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

有了这个目标 api 对我来说效果很好