我在我的应用程序中实现了 DayNight 主题并添加了一个设置以在白天和夜间模式之间切换,但我无法在不重新启动的情况下动态切换模式。
如果我在更改设置后使用 setDefaultNightMode()
,设置 Activity 不会更改模式,但后台堆栈中的 Activity 会更改。如果我另外使用 setLocalNightMode()
设置 Activity 会重新创建并更改其模式,但现在后台堆栈中的 Activity 不会。我无法找到一种方法来完成两者。有办法做到这一点吗?
最佳答案
这里是CheeseSquare 的MainActivity.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/