Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pressing on notification opens the same activity twice

If i have an activity that closed by a user (user pressed on home so the app is still in app stack) and then he gets a notification, when he presses on it i start an activity and now the same activity is opened twice. How can i prevent this from happening?

My CODE:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
        new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

NotificationManager mNotificationManager = (NotificationManager)
        this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Title")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setAutoCancel(true)
            .setLights(GCMSIntentService.PURPLE, 500, 500)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentText(msg);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());
like image 685
ilan Avatar asked Feb 11 '23 10:02

ilan


1 Answers

android:launchMode="singleTask"

in manifest Or, use it as flag for your intent.

like image 60
suresh Avatar answered Feb 13 '23 06:02

suresh