带有 configChanges 的 Android DayNight 主题

Dev*_*oid 5 android rotation orientation

我正在创建一个应用程序,在其中使用 Android 支持库的主题 DayNight。

这是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>
Run Code Online (Sandbox Code Playgroud)

我在清单中使用 configChanges 来管理轮换。

android:configChanges="keyboardHidden|orientation|screenSize"
Run Code Online (Sandbox Code Playgroud)

在我的 AppcompatActivity 上的 onCreate 中,我添加了:

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
Run Code Online (Sandbox Code Playgroud)

问题是,当我使用 setDefaultNightMode 时,android:configChanges 停止工作,并在每次轮换中重新创建活动。

欢迎任何形式的帮助

小智 3

可能您需要将“ uiMode ”参数添加到清单中:

android:configChanges="orientation|screenSize|uiMode|keyboardHidden"
Run Code Online (Sandbox Code Playgroud)