7

The service creates a persistent Notification and starts the main activity on click via PendingIntent. Here is the code.

Intent notificationIntent = new Intent(getApplicationContext(), ViewPagerActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(StreamingService.this, 0, notificationIntent, 0);

However, if the main activity is started when I press notification, it will be started again, and again and...

In the end, I have the same activity pilled up, one on the top of another. I can see this by pressing the back button, which will kill the Main activity once and then get me back to the same activity until I close the last one.

How can I prevent this to happen? Can PendingIntent detect that aiming activity is running so it does not create the same activity again, but rather start the running one?

PS. I apologize if not being able to explain this well. If this is the case, let me know and I will rephrase the problem.

sandalone
  • 41,141
  • 63
  • 222
  • 338
  • Have a look at [this post](http://stackoverflow.com/a/5631993/593709) – Adil Soomro Dec 18 '12 at 13:34
  • 1
    This question really has nothing to do with notifications; they are merely the mechanism for launching your activity but your problem would occur with any other mechanism. @etienne's answer contains what you need to fix it; another option is to use the android:launchMode attribute in your manifest to properly set your launch mode for this activity. – mah Dec 18 '12 at 13:35
  • @mah you're right. I will fix this. – sandalone Dec 18 '12 at 13:43

3 Answers3

8

I also found this solution. Add this attribute to Manifest

           <activity android:name=".MyActivity"
              android:label="@string/app_name"
              android:launchMode="singleTop"      // <-- THIS LINE
            >

for each Activity you need this feature. So far it work with no errors at all.

Which solution is better? Mine is easier, if nothing.

Roman Holzner
  • 5,738
  • 2
  • 21
  • 32
sandalone
  • 41,141
  • 63
  • 222
  • 338
  • 1
    Sounds good, too. The previous solution (Intent flags) would be useful if you want to use different options for different cases; ie. depending on the user action, you may want to start the activity in a different way. It mainly depends on your application navigation schema, I guess. – etienne Dec 20 '12 at 09:41
  • +1 useful for me. To make this answer better check this out http://developer.android.com/guide/topics/manifest/activity-element.html it's explain who it's work single top and catch this intent on the activity's method onNewIntent. – BlaShadow Apr 19 '14 at 17:40
6

Depending on the exact behaviour you want to implement, you could pass one of these flags as the last param of getActivity():

Justin Muller
  • 1,283
  • 13
  • 21
etienne
  • 3,146
  • 1
  • 24
  • 45
1

Try using another value in REQUEST_CODE. don't use default value 0 in REQUEST_CODE.

or your pendingIntent will restart your activity.

if you want to use normal activity cycle, input request value when creating PendingIntent.

PendingIntent resultPendingIntent = PendingIntent.getActivity(
        context, REQUEST_CODE,
        resultIntent,
        PendingIntent.FLAG_UPDATE_CURRENT);
JunsungChoi
  • 87
  • 2
  • 15
  • "don't use default value 0 in REQUEST_CODE.or your pendingIntent will restart your activity." please share the source – pvllnspk Jun 23 '16 at 13:46