user3088957
user3088957

Reputation: 21

PendingIntent on Notification foreground service android

I have an activity in my app. When i click one option, start running my service foreground with a notification like this:

PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0,
            new Intent(getApplicationContext(), Radio.class),
            PendingIntent.FLAG_UPDATE_CURRENT);

    Notification  notification = new Notification();
    notification.tickerText = "Connecting RadiO!.. Wait..";
    notification.icon = R.drawable.ic_launcher;
    notification.flags |= Notification.FLAG_ONGOING_EVENT;
    notification.setLatestEventInfo(getApplicationContext(), "radiO!",
            "Playing: " + songName, pi);
    startForeground(1, notification);

Then, still running my activity, I click on my notification and I opened it without closing the previous activity. I must not have the same activity twice.

Upvotes: 1

Views: 2681

Answers (1)

Martin Cazares
Martin Cazares

Reputation: 13705

What you are doing is creating a new instance of the activity you are trying to execute each time you click on the notification, in order to avoid it and reuse an already existing activity(if any), just set the activity as singleTask in the AndroidManifest.xml as shown below:

    <activity
       android:name=".YourActivity"
       android:launchMode="singleTask"/>

Hope this helps.

Regards!

Upvotes: 4

Related Questions