恢复后如何返回到同一个活动实例?

发布于 2024-11-15 11:03:43 字数 495 浏览 6 评论 0原文

这个问题有点奇怪,在尝试解决了大约一天之后,我将其发布在这里。 我有一个应用程序,其中活动 A(主要活动)启动其他活动(B、C 或 D)。 当活动 A 启动活动 B 并按下“主页”按钮时,就会出现此问题。

案例 1 - 当我在设备 (HTC Desire) 上以调试模式测试应用程序时,按“主页”按钮后,我再次单击应用程序图标,它返回到相同的活动(活动 B),这是应该做的。这里没有问题。

情况 2 - 当我导出签名的包,然后在同一设备上安装应用程序时,如果我在按“主页”按钮后单击应用程序图标,则会在顶部启动活动 A(主活动)的新实例我之所以知道这一点,是因为当我从该活动按“后退”时,它会返回到活动 B,并再次按“后退”显示活动 A。

如果应用程序以与它相同的顺序退出,则该行为将不再存在。已开始,也就是说,如果我按从活动 B“返回”,然后从活动 A“返回”(退出)。

此后一切正常。 我已经用不同的设置对其进行了多次测试,但我似乎无法弄清楚为什么会出现这样的行为。

任何帮助表示赞赏。

The problem is somewhat odd and after having trying to figure it out for about a day now, I am posting it here.
I have an application where an activity A(main activity) launches other activities(B,C or D).
The issue here happens when activity A has started Activity B and 'home' button is pressed.

Case 1 - When I test my application in debug mode on my device (HTC Desire) after pressing the 'home' button, I again click the application icon, it returns to the same activity (activity B), which is what is should do. No issues here.

Case 2 - When I export the signed package, and then install the application on the same device, then if I click the application icon after pressing the 'home' button, then a new instance of activity A (main activity) is launched ON TOP of activity B. I got to know this because when I press 'back' from that activity, it returns to activity B and pressing 'back' again shown activity A.

The behavior ceases to exist if the application is quit in the same order it was started, that is, if I press 'back' from activity B, then 'back' from activity A (exit).

After this everything runs fine.
I have tested it many times with different settings but I can't seem to figure out why the behavior is like this.

Any help is appreciated.

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

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

发布评论

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

评论(4

我恋#小黄人 2024-11-22 11:03:43

我认为在清单中为 Activity A 添加“单顶”标志应该可以解决此问题。

I think giving Activity A the 'single top' flag in your manifest should fix this.

羁客 2024-11-22 11:03:43

关于案例1:
从 Activity A 启动意图以启动 Activity B 时,请添加标志 FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET< /a>
这将确保当您回家并再次启动应用程序时,将显示活动 A。

关于案例2:
我不太确定这是如何发生的。它似乎认为您有两个版本的应用程序,已签名的版本和未签名的版本,但将它们都保留在同一任务堆栈中。如果您只需要一个实例,您可能需要考虑为您的活动使用 singleTask 或 singleInstance。请参阅有关任务和返回堆栈 了解更多详情。

Regarding Case 1:
When launching your intent from Activity A to start Activity B, add the flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET
This will ensure that when you go home and launch the the app again, Activity A will be shown.

Regarding Case 2:
I'm not exactly sure how this would occur. It seems like it thinks you have two versions of the app, the signed one and the unsigned one but keeps them both in the same task stack. You may want to consider using singleTask or singleInstance for your Activity if you only want one instance. See the doc on tasks and back stack for more details.

遗弃M 2024-11-22 11:03:43

关于情况 2 的可能原因,我同意 Noel 的观点。如果没有任务重定父级或将其设置为阻止活动的多个实例的启动模式,则从 Home 启动它可能不会被视为与从 Home 启动它的堆栈相同Eclipse(假设情况如此)。

在我的人才计算器应用程序中,我将整个应用程序设置为allowTaskReparenting=true,以确保其他堆栈中没有留下任何内容(主要是电子邮件,因为它可以通过电子邮件发送启动网址)。然后,我将主要活动设置为 launchMode="singleTask",因为我只希望存在一个实例,无论是什么启动它或出于任何意图。

我唯一的其他活动是加载和保存,并且没有History =“true”以确保它被删除并且永远不会返回。这基本上意味着它只在你身处其中时存在,并且永远无法返回。

clearTaskOnLaunch="true" 还将确保从主页启动时仅主 Activity 保留在堆栈中,但如果您有其他方式进入 Activity,则情况并非总是如此。如果它仅从家庭启动,则设置此项。

希望一切有帮助。

I would agree with Noel regarding the likely cause of Case 2. Without task reparenting or it being set to a launchmode preventing multiple instances of an activity, there is a chance that launching it from Home isn't deemed the same stack as launching it from Eclipse (assuming this to be the case).

In my talent calculator app I have the whole application set allowTaskReparenting=true to ensure nothing is left in other stacks (primarily email as it can email launch urls). I then have my main activity set to launchMode="singleTask" as I only ever want one instance of this to exist no matter what launches it or with whatever intent.

My only other activity is for loading and saving and that has noHistory="true" to make sure it is removed and never returned to. That basically means it only exists while you're in it, and can never return to it.

clearTaskOnLaunch="true" will also ensure only the main Activity remains in the stack when it's launched from Home, but this isn't always the case if you have other ways to get into your activity. If it's only ever launched from Home then set this.

Hope that all helps.

溇涏 2024-11-22 11:03:43

您是手动启动应用程序还是使用 Eclipse 或其他 IDE 启动应用程序?当从 Intellij IDEA 开始时,我遇到了完全相同的问题。然后我停下来手动运行它,行为符合预期。

Do you start you application manually or using Eclipse or another IDE? When starting from Intellij IDEA I had exactly the same problems. Then I stopped and ran it manually and behaviour was as expected.

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