简体   繁体   English

单击通知时如何关闭我的应用程序?

[英]How to close my application when clicking on a notification?

I created notifications and when you click on the notification does a startActivity. 我创建了通知,当您单击通知时会执行startActivity。 This works perfectly, especially when my application has not been started. 这非常有效,尤其是在我的应用程序尚未启动时。

The problem I have is when I open the notification since the previous activity remains open. 我的问题是由于上一个活动仍处于打开状态,因此当我打开通知时。

My intention is that when you click on the notification to close the current application and this contain only the new call from the notification. 我的意图是,当您单击通知以关闭当前应用程序时,它仅包含来自通知的新呼叫。

I've tried showing in the "AndroidManifest" with "android:launch=" and flags on PendingIntents but I haven't successfully run it. 我试过在“ AndroidManifest”中显示“ android:launch =”和PendingIntents上的标志,但是我没有成功运行它。

Any help? 有什么帮助吗? thanks 谢谢

EDIT: My application starts as follows: 编辑:我的应用程序启动如下:

SplashScreen.class -> Main.class (is a TabActivity) SplashScreen.class-> Main.class(是一个TabActivity)

When I click on the notification I do the following: SplashScreen.class -> Main.class (is a TabActivity) 当我单击通知时,我将执行以下操作:SplashScreen.class-> Main.class(是TabActivity)

But if my application is already started and I pulse on the notification, I have: 但是,如果我的应用程序已经启动,并且我在通知中脉动,则我有:

NEW - SplashScreen.class -> Main.class (is a TabActivity) (Current) OLDER. 新增-SplashScreen.class-> Main.class(是TabActivity)(当前)旧。 And below is still open above Main.class 并且下面仍然在Main.class上面打开

If your need is to Open SplashScreen.class when you click notification try to create an intent like this: 如果您需要在单击通知时打开SplashScreen.class,请尝试创建如下所示的意图:

Intent intent = new Intent(myContext, SplashScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

as documentation says for Intent.FLAG_ACTIVITY_CLEAR_TOP 如文档所述,表示Intent.FLAG_ACTIVITY_CLEAR_TOP

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent. 如果已设置,并且正在启动的活动已经在当前任务中运行,那么与其启动该活动的新实例,不如关闭该活动之上的所有其他活动,并将此Intent传递给(现在顶部)将旧活动作为新的Intent。

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B. 例如,考虑一个由以下活动组成的任务:A,B,C,D。如果D调用具有解析为活动B组件的Intent的startActivity(),则C和D将完成,并且B接收给定的Intent ,导致堆栈现在为:A,B。

The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. 在上面的示例中,活动B的当前正在运行的实例将通过onNewIntent()方法接收您在此处开始的新意图,或者自行完成并使用新意图重新启动。 If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; 如果已将其启动模式声明为“多个”(默认),并且您未将FLAG_ACTIVITY_SINGLE_TOP设置为相同的意图,则它将完成并重新创建;否则,它将重新创建。 for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent(). 对于所有其他启动模式,或者如果设置了FLAG_ACTIVITY_SINGLE_TOP,则此Intent将被传递到当前实例的onNewIntent()。

so when you launch SplashScreen activity with this flag, if it already is on the stack it will be brought to the top and all other activities above it on the stack will be cleared 因此,当您使用此标志启动SplashScreen活动时,如果该活动已经在堆栈中,它将被带到顶部,并且堆栈上位于其上方的所有其他活动将被清除。

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

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