android - 无法在 DayNight 主题中动态切换模式

标签 android themes android-support-library

我在我的应用程序中实现了 DayNight 主题并添加了一个设置以在白天和夜间模式之间切换,但我无法在不重新启动的情况下动态切换模式。

如果我在更改设置后使用 setDefaultNightMode(),设置 Activity 不会更改模式,但后台堆栈中的 Activity 会更改。如果我另外使用 setLocalNightMode() 设置 Activity 会重新创建并更改其模式,但现在后台堆栈中的 Activity 不会。我无法找到一种方法来完成两者。有办法做到这一点吗?

最佳答案

这里是CheeseSquareMainActivity.java 模块中的实现。 repo 位于here :

private void setNightMode(@AppCompatDelegate.NightMode int nightMode) {
    AppCompatDelegate.setDefaultNightMode(nightMode);

    if (Build.VERSION.SDK_INT >= 11) {
        recreate();
    }
}

这里是从 V25 开始对 recreate() 的描述。我似乎无法找到此调用的其他文档 - 请注意它是在 V11 中添加的。

/* Cause this Activity to be recreated with a new instance.  This results
 * in essentially the same flow as when the Activity is created due to
 * a configuration change -- the current instance will go through its
 * lifecycle to {@link #onDestroy} and a new instance then created after it.
 */

关于android - 无法在 DayNight 主题中动态切换模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41724748/

相关文章:

android - QT Android 应用程序应该在 android 启动日志之后启动

android - 在 native Android 应用程序中使用 libssl.so

android - 我如何从 styles.xml 设置 RelativeLayout 的样式 - Android

c# - 使用主题时的数据模板不起作用 - WPF

java - 通过android studio将嵌入式条形码扫描仪添加到 fragment 到我的应用程序中

java - 运行时异常 : Unable to instantiate receiver

WPF 主题最佳实践

android - actionButtonStyle 不适用于支持库 v23.1.0

Android:底部导航 View - 更改所选项目的图标

android - "Cannot resolve class android.support.v4.widget.DrawerLayout"即使在 gradle 中添加依赖项之后