Reputation: 11050
Service
A creates a Notification
with a PendingIntent
that opens Activity
B. User pulls the notification drawer down and presses the Notification
with Activity
B on front previously. The PendingIntent
basically adds another instance of Activity
B instead of opening the currently open one.
Is there any way to make the PendingIntent
open the Activity
if it's already open, go back on the backstack if there's an instance of that Activity
there, and open a new instance otherwise?
Upvotes: 1
Views: 1065
Reputation: 812
You can set Activity's attribute in Manifest file as following:
android:launchMode="singleTask"
or
On defining Flags for PendingIntent, you can use flags as:
Intent in=new Intent();
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
Upvotes: 0
Reputation: 95578
Add Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
when creating the Intent
for your Notification
.
Setting both Intent.FLAG_ACTIVITY_CLEAR_TOP
and Intent.FLAG_ACTIVITY_SINGLE_TOP
will cause an existing instance of the Activity
to be used (onNewIntent()
will be called). If you only specify Intent.FLAG_ACTIVITY_CLEAR_TOP
and there is already an existing instance of the Activity
, that instance (and all other activities on top of it) will be finished and removed from the stack and a new instance of the Activity
will be created (onCreate()
will be called).
Upvotes: 2
Reputation: 15379
Yes, you can use the single task or single instance launch mode of your activity (or in your pending intent flags) to control this behavior.
Check the launch modes documentation for details on how to set those flags.
Upvotes: 0