正在 pendingIntent 从堆栈中移除启动器 activity

Removing launcher activity from stack on pendingIntent

我有一些问题。当我的应用程序不在后台(从最近删除)时,一切正常。但是当我的应用程序是最近的,然后我通过通知挂起意图打开 "ResponseActivity" 时,在 "ResponseActivity" 的后退单击我进入我的 MainActivity(启动器 activity)。 我添加了 FLAG_ACTIVITY_NEW_TASK 但它似乎没有添加。

    Intent intent = new Intent(this, ResponseActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP   | Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.drawable.ic_launcher_background)
            .addAction(0, "Other", pendingIntent) 
            .setLargeIcon(getCircleBitmap(bitmap))
            .setContentTitle(userDB.getName())
            .setContentText(smallText)
            .setAutoCancel(true)
            .setDefaults(Notification.DEFAULT_ALL)
            .setColor(getResources().getColor(R.color.colorPrimary))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT);


    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);

        channel.setDescription("");
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        channel.enableVibration(true);

        mNotificationManager.createNotificationChannel(channel);
    }

    mNotificationManager.notify(1000, mBuilder.build());

清单:

<application
    android:name=".ApplicationContextProvider"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".MyFirebaseInstanceService"
        android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>

    <activity android:name=".MainActivity" />
    <activity android:name=".LoginActivity" />

    <activity android:name="verification.MyVerifyPhoneActivity" />
    <activity android:name=".ResponseActivity"
        android:theme="@style/AppTheme2">

    </activity>
</application>

如果您想确保在 select 通知时只有 ResponseActivity 在堆栈中,您可以这样做:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

而不是:

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);