1

I have an activity which plays music and shows notification

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK );
            mBuilder.setContentIntent(pendingIntent);
  mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    final int mId = 1;
            mNotificationManager.notify(mId,mBuilder.build());

the problem is that as I go back from this activity lets say activity B, and I tap on the notification the activity restarts and now I have two streams of music playing, it seems as if the activity is duplicated

I had a look into the manifest to see if I can change something, and I added this line to my activity

android:launchMode="singleTop" 

this didn't help either, any ideas? Thanks!

user2529058
  • 83
  • 3
  • 12
  • what do you just want to click the notification and it goes away, thats it? – JRowan Jun 29 '13 at 11:11
  • when I click on the notification I want the activity which threw the notification to open up where it left off, and not restart the whole mediaplayer and play the song again while one is already playing in the background – user2529058 Jun 29 '13 at 11:16
  • PendingIntent.FLAG_UPDATE_CURRENT in your pending intent i think, where you have flag_activity_new_task – JRowan Jun 29 '13 at 11:17
  • use android:launchMode="singleTask" – stinepike Jun 29 '13 at 11:21
  • FLAG_UPDATE_CURRENT cannot be resolved or is not a field :o? – user2529058 Jun 29 '13 at 11:23
  • android:launchMode="singleTask" still creates a new activity – user2529058 Jun 29 '13 at 11:25
  • it was just a guess from another post i seen i never tried it, it said PendingIntent.FLAG_UPDATE_CURRENT not just FLAG_UPDATE_CURRENT StinePike is probably right – JRowan Jun 29 '13 at 11:25
  • StinePike trick doesn't work, and pendingintent.FLAG_UPDATE_CURRENT needs an assignment operator! what do I assign it to? – user2529058 Jun 29 '13 at 11:29
  • it should work just like this, is this how you have it PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT ); – JRowan Jun 29 '13 at 11:34
  • :( nope! doesn't work – user2529058 Jun 29 '13 at 11:36
  • check out the answer to this, this is probably what you want http://stackoverflow.com/questions/13934061/prevent-notification-pendingintent-starts-activity-already-started – JRowan Jun 29 '13 at 11:38
  • Playing music wold be done by `MediaPlayer`, I think. So just stop the player on `onPause` or `onStop` of your activity. – AnujMathur_07 Jun 29 '13 at 11:44

1 Answers1

-3

Please create global variable of Activity and check if it is not null then variable.finish() and launch the activity.

Activity globalVariable;
if(globalVariable!=null) {
    globalVariable.finish();
}
Intent in = new Intent(this,b.class);
startActivity(in);
n1ckolas
  • 4,380
  • 3
  • 37
  • 43
nilesh patel
  • 834
  • 1
  • 5
  • 10