简体   繁体   中英

How to close my application when clicking on a notification?

I created notifications and when you click on the notification does a 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.

Any help? thanks

EDIT: My application starts as follows:

SplashScreen.class -> Main.class (is a TabActivity)

When I click on the notification I do the following: SplashScreen.class -> Main.class (is a 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. And below is still open above Main.class

If your need is to Open SplashScreen.class when you click notification try to create an intent like this:

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

as documentation says for 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.

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.

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. 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; 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().

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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