按通知两次打开相同的活动

ila*_*lan 4 android android-activity android-pendingintent google-cloud-messaging

如果我有一个被用户关闭的活动(用户按下主页,所以应用程序仍在应用程序堆栈中),然后他收到通知,当他按下它时,我开始一个活动,现在同一个活动打开了两次。我怎样才能防止这种情况发生?

我的代码:

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());
Run Code Online (Sandbox Code Playgroud)

小智 6

android:launchMode="singleTask"
Run Code Online (Sandbox Code Playgroud)

在清单中或者,将其用作您意图的标志。