简体   繁体   English

在有目的的情况下启动活动之前,请清除活动堆栈

[英]Clear the activity stack before launching activity with intent

I'm working on C2DM notification for an Android Application and I'd like to open my application when user click on the notification. 我正在处理Android应用程序的C2DM通知,当用户单击通知时,我想打开我的应用程序。 There is no problem for that, this is pretty easy. 这样做没有问题,这很容易。 The problem is that when the application is launching (after clicking on the notification), if some activity was previously opened, the launched activity seems to be added to the actual activity stack, what is a problem regarding to the complexity of my application (there is a lot of activity, some with static fields). 问题在于,当应用程序启动时(单击通知后),如果先前已打开某些活动,则启动的活动似乎已添加到实际的活动堆栈中,这与我的应用程序的复杂性有关(存在是很多活动,其中一些活动包含静态字段)。

To solve the problem, 2 solutions would be OK: 要解决该问题,可以采用以下两种解决方案:

1) Do not call a specific activity but just ask to my application to open (like when I click on the application icon on the home screen: Open the first activity if the application was closed or just bring the application to the front if was opened (but was in background)). 1)不要调用特定活动,而只是要求打开我的应用程序(例如,当我单击主屏幕上的应用程序图标时:如果应用程序已关闭,则打开第一个活动;如果已打开,则将其置于最前面(但在后台)。

2) Clear all the activity stack and launch a specific activity. 2)清除所有活动堆栈并启动特定活动。

But I didn't succeed to do one of both solution. 但是我没有成功做这两种解决方案之一。 Even using intent flag (like http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP ). 即使使用意图标志(例如http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP )。

Can somebody help me to solve this problem? 有人可以帮我解决这个问题吗?

Thanks 谢谢

PS: Sorry for my poor English, I'm from Belgium :-) PS:对不起,我的英语不好,我来自比利时:-)

It's not what you asked to do but you can add the attribute android:launchMode="singleTask" to the activity you will be calling out of this notification and it won't create a new activity if one this instance already exists. 这不是您要执行的操作,但是您可以将属性android:launchMode="singleTask"到要从此通知中调用的活动中,并且如果此实例已经存在,则不会创建新活动。

You could possibly also use the ActivityManager.killBackgroundProcesses(String packageName) to remove background processes but I have never tried this and it isn't advised or use the ChriZzZ suggestion and manage your activities a bit tighter. 您可能还可以使用ActivityManager.killBackgroundProcesses(String packageName)删除后台进程,但是我从未尝试过这样做,因此不建议这样做,也可以使用ChriZzZ建议并更加严格地管理您的活动。

Sounds like you are searching for FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET 听起来好像您在搜索FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET

If set, this marks a point in the task's activity stack that should be cleared when the task is reset. 如果设置,则标记任务活动堆栈中的一个点,该点在重置任务时应清除。 That is, the next time the task is brought to the foreground with FLAG_ACTIVITY_RESET_TASK_IF_NEEDED (typically as a result of the user re-launching it from home) 也就是说,下次使用FLAG_ACTIVITY_RESET_TASK_IF_NEEDED将任务带到前台(通常是由于用户从家中重新启动它)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM