Android:如何从活动堆栈中完全删除活动?
我有两个活动 FirstActivity 和 SecondActivity。
FirstActivity 有一个意图过滤器 MAIN、LAUNCHER 和 DEFAULT。 SecondActivity 只是一个普通的 Activity,AndroidManifest 中没有设置其他标志。
我的应用程序启用了 C2DM,当收到通知时,会显示任务栏图标,单击时会打开 SecondActivity。
通知到达有两种情况: 首先,当通知到达时,我的应用程序已经在运行;其次,我的应用程序完全没有运行。
在第一个场景中,一切都很好。收到通知,我从通知下拉列表中单击,然后显示我的 SecondActivity。当我按回时,SecondActivity 已完成,应用程序将转到后台。当我从最近的应用程序再次启动我的应用程序时,它显示 FirstActivity 这是正确的,因为它是 MAIN、LAUNCHER 和 DEFAULT。
在第二个场景中,收到通知,我从通知下拉列表中单击它,然后显示我的 SecondActivity。当我按回时,SecondActivity 已完成,应用程序将转到后台。当我从最近的应用程序再次启动我的应用程序时,它显示 SecondActivity 这是错误的。我期待 FirstActivity 能够显示,因为它应该是一个新的开始。
我在第二种情况后尝试了 ForceKilling 应用程序,但结果是相同的,我需要重新启动手机才能再次从 FirstActivity 启动。
大家有遇到同样的问题吗?或者只是我?您认为我的配置有什么问题?
我也尝试将 noHistory=false 设置为 SecondActivity 但结果仍然相同。
I have two Activities FirstActivity and SecondActivity.
FirstActivity has an intent filter MAIN, LAUNCHER and DEFAULT.
SecondActivity is just a normal activity and no other flags is set in the AndroidManifest.
My application is C2DM enabled and when a notification is received a taskbar Icon is displayed and when clicked it opens SecondActivity.
Arrival of Notifications has two scenarios:
First, My Application is Already Running when notification arrived and second My Application is totally not running.
On the first scenario, everything is fine. A notification is received, i clicked from the notification drop down and my SecondActivity is displayed. When I press back SecondActivity is finished and application goes to the background. When I start my application again from Recent Applications, it displays FirstActivity which is correct since its the MAIN, LAUNCHER and DEFAULT.
On the Second Scenario, A notification is received, I clicked it from the notification drop down and my SecondActivity is displayed. When I press back SecondActivity is finished and application goes to the background. When I start my application again from recent Applications, it displays SecondActivity which is wrong. I am expecting FirstActivity to be displayed because it should be a fresh start.
I tried ForceKilling the application after the second scenario but the result is the same, I needed to restart my phone to be able to start from FirstActivity Again.
Have you guys encountered the same problem? or is it just me? What do you think is wrong with my configuration?
I also tried setting noHistory=false to SecondActivity but still the results are the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当您从“最近的应用程序”快捷方式启动应用程序时,它将启动最后一个活动。因此您可以尝试从应用程序列表中启动该应用程序,看看能否解决您的问题。
When you start application from 'Recent Application' shortcut, it will launch the last activity. So you can try to launch the application from the application list, see whether it solves your problem.
这是正确的行为..如果您通过“最近的应用程序”启动应用程序,它将指向最后一个活动。
尝试通过普通应用程序列表启动它
It is the correct behavior.. if you start the application through "Recent Application" it will point from the last activity.
Try launching it through normal application list
对于您的主 Activity,请尝试在 AndroidManifest 文件中将启动模式设置为“singleInstance”。对于第二个活动,将“完成任务启动”设置为 true。
For your main Activity try to set the lauch mode as "singleInstance" in the AndroidManifest file. for second activity set "Finish On Task launch" to true.
在第二种情况下,我认为随着应用程序关闭,活动堆栈为空。所以现在您正在启动 Activity2 ,意味着这是 stack 中唯一的 Activity 。现在,如果是第二种情况,可以通过覆盖 onbackpress 并启动 Activity1 来实现所需的后退行为。
In Second scenario i think activity stack is empty as app is closed . so now you are starting activity2 , means this is the only activity in stack . now desired back behavior can be possible by overriding onbackpress and start activity1 if its second scenario .
您可以使用没有设置这样的历史记录的标志来调用您的第二个活动,
这对我有用过几次。
阅读 Android 文档
you can call your second activity with the flag set no history like this,
This has worked for me several times.
Read from the Android Doc