后台服务的 startActivity 会导致奇怪的任务切换行为

发布于 2024-12-10 22:13:54 字数 494 浏览 4 评论 0原文

我的应用程序主要由两个活动 A 和 B 以及一个后台服务(由警报启动)组成。 Activity A 是启动器(主要)活动。 Activity B 时不时地由后台服务启动,并设置 FLAG_ACTIVITY_NEW_TASK(这是在 bg 服务中启动 Activity 所必需的)。

期望的行为:每当应用程序离开(返回主屏幕、后退按钮等)并且用户返回时,Activity A 就会恢复。

我的问题:每当后台服务启动 B 时,应用程序都会始终通过活动切换器(长按主页按钮)返回到 B,无论我是否是否通过后退按钮关闭了它。然后,如果我选择应用程序的主页图标,我只能返回到活动 A(因为随后 A 将作为启动活动启动)。

我认为这与 B 由后台服务在新任务中启动,并且应用程序切换器尝试返回到这个新任务有关。但是当我通过后退按钮关闭 B 返回到 A 时,新任务不应该结束吗?

有人可以向我解释一下这是怎么回事吗?

My app consists mainly of two activities, A and B, and a background service (started by alarms).
Activity A is the launcher (main) activity.
Activity B is started every now and then by the background service, with FLAG_ACTIVITY_NEW_TASK set (this is required for activity starts in bg services).

Desired behaviour: Whenever the app is left (return to homescreen, backbutton etc.) and the user returns to it, Activity A shall be resumed.

My problem: Whenever B has been started by the background service, the app will always return to B through the activity switcher (home-button long press), no matter if I have closed it via back-button or not. I can then only return to Activity A if I select the home icon of the app (because A is then started as launch activity).

I think this has to do with fact that B is started in a new task by the background service, and the app-switcher tries to return to this new task. But shouldn't the new task end when I close B via back-button, returning to A?

Can someone please explain to me what's the matter here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事风中埋 2024-12-17 22:13:54

完整且最终的解决方案是将以下代码添加到服务打开的 menifest 文件中的活动中。

android:taskAffinity="" 
android:excludeFromRecents="true" 
android:launchMode="singleInstance"

The full and final solution is to add the below code to the activity in the menifest file which is opened by the service.

android:taskAffinity="" 
android:excludeFromRecents="true" 
android:launchMode="singleInstance"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文