Charlie-Blake
Charlie-Blake

Reputation: 11050

Make PendingIntent from notification not fire if the Activity is already on front

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

Answers (3)

pratiti-systematix
pratiti-systematix

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

David Wasser
David Wasser

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

Greg Ennis
Greg Ennis

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

Related Questions