我在我的应用程序中实现了 DayNight 主题,并添加了一个在白天和夜间模式之间切换的设置,但如果不重新启动,我无法在模式之间动态切换。
如果我使用setDefaultNightMode()
设置更改后,设置活动不会更改模式,但后台堆栈中的活动会更改模式。如果我另外使用setLocalNightMode()
设置活动被重新创建并更改其模式,但现在后台堆栈中的活动不会。我找不到一种方法来实现这两者。有没有办法做到这一点?
这是在中的实现MainActivity.java
的模块奶酪广场 https://github.com/chrisbanes/cheesesquare回购协议位于here https://github.com/chrisbanes/cheesesquare/tree/master/app/src/main/java/com/support/android/designlibdemo:
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.
*/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)