即使有人从黑暗模式切换到白天模式或反之亦然,我如何保持我的 Android 活动继续进行(使用 Java)
问题是,当我从深色模式切换到浅色模式(android 深色模式和 android 浅色模式)时,正在进行的活动工作正常,但重复的活动会重新开始。
现在,当您仅处理视觉效果时,这并不重要,因为新活动覆盖屏幕并且旧活动不会显示,但我也在处理声音,因此可以听到两个活动中播放的声音。
简而言之,当从深色模式切换到浅色模式(或反之亦然)时,正在进行的活动会继续,而同一活动的重复活动将从头开始。我不想要这个新的重复活动。我该如何避免呢?
我认为这里不需要我的代码。我需要 Java 的答案。
The problem is that when I switch from dark mode to light mode (android dark mode and android light mode), the activity which was going on that works fine but a duplicate activity starts all over again.
Now this does not matter when you are just working with visuals because the new activity covers the screen and old activity is not displayed but I am also working with sounds so the sounds playing in both the activities are heard.
In short, when switching from dark mode to light (or vice versa), the ongoing activity continues while a duplicate activity of the very same one starts from the beginning. I don't want this new duplicate activity. How do I avoid it?
I don't think my code is required here. I require the answer in Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

你的问题描述没有多大意义。通常,如果您更改亮/暗模式设置,Android 将杀死您的
Activity
的现有实例并创建一个新实例,以便新实例可以使用正确的样式重新创建所有 UI 元素。你说 Android 并没有杀死现有实例,而是启动了一个新实例,这是没有意义的。您应该在onCreate()
和onDestroy()
中添加日志记录,以查看是否确实发生了这种情况,或者您是否看到了其他情况。来阻止 Android 重新启动您的
Activity
请注意,您可以通过在清单中添加
声明。 Android 框架不会终止并重新创建您的
Activity
,而是会对Activity
的现有实例调用onConfigurationChanged()
。然后您需要自己处理更改。The description of your problem doesn't make much sense. Normally, if you change the light/dark mode setting, Android will kill the existing instance of your
Activity
and create a new instance, so that the new instance can recreate all the UI elements with the proper style. You say that Android isn't killing the existing instance, but it is starting a new instance and this doesn't make sense. You should add logging inonCreate()
andonDestroy()
to see if this is really going on, or if you are seeing something else.Note that you can prevent Android from restarting your
Activity
by addingto the
<activity>
declaration in the manifest.Instead of killing and recreating your
Activity
, the Android framework will instead callonConfigurationChanged()
on the existing instance of theActivity
. You will then need to handle the change yourself.