finish activity in which a pending intent starts
Asked Answered
G

2

6

In my app, i created a custom notification that contain a button that show recent apps(instead of home button long press)

when user open main activity and press home button to go to home screen then drag the notification drawer and click on the button:

Desired result is that the recent apps are shown and the main activity is one of the recent apps.

Actual result is that the recent apps are shown but the main activity is not in them and the main activity resumes.

My code to start "RecentApps (is a dummy class that show recent apps)" class with pending intent

    Intent recentAppIntent = new Intent(getBaseContext(), RecentApps.class);
    PendingIntent pendingrecentAppIntent = PendingIntent.getActivity(getBaseContext(), 1, recentAppIntent, 0);
    notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);

I think that the problem is getBaseContext, so when the main activity is alive and not finished the recent apps are shown but with context is the main activity.

I tried getApplication and getApplicationContext but not working.

I also tried to use flags for "recentAppIntent" but it is not working, It is a half solution to use

recentAppIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

but it not what i need.

So, my question is "How to finish the main activity in which the pending intent starts".

Thanks in advance, Mostafa

Gavingavini answered 27/2, 2014 at 2:58 Comment(2)
The Context you use in calls to PendingIntent.getActivity() or new Intent() are not relevant to your problem (in other words, it makes no difference what Context you use). Otherwise I don't understand your problem. Can you explain better what you want to happen and what is actually happening?Bramwell
Thanks for reply, what i want: when open the main activity of my app and go to home screen using home button and click on "recent apps" button from my custom notification, my app should be included in the recent opened apps. Actually happening : my app not included in the recent apps and the main activity resumes, when recent apps disappear using the back button, the main activity appear.Gavingavini
B
5

You need to ensure that your MainActivity and your RecentApps don't belong to the same task. The "Recent apps" doesn't actually show recent apps. It shows recent tasks.

To make sure that your RecentApps isn't in the same task as your MainActivity, you can add the following to the <activity> definition in the manifest for RecentApps:

aandroid:taskAffinity=""

Also, when creating the notification, add FLAG_ACTIVITY_NEW_TASK to the Intent, like this:

Intent recentAppIntent = new Intent(getBaseContext(), RecentApps.class);
recentAppIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingrecentAppIntent = PendingIntent.getActivity(getBaseContext(), 1, recentAppIntent, 0);
notificationView.setOnClickPendingIntent(R.id.recentAppButt, pendingrecentAppIntent);
Bramwell answered 3/3, 2014 at 22:7 Comment(2)
yeeeeeessssss, it works correctly. Thanks very match, I can't explain my happiness. Thank you again.Gavingavini
sorry, I can't vote up the answer because i am new in stack-overflow.Gavingavini
C
2

I think FLAG_ACTIVITY_CLEAR_TOP will help as a flag.

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Explanation for Intent flags are available here: link

Carmel answered 13/6, 2018 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.