在pendingIntent上从堆栈中删除启动器活动

时间:2018-11-21 14:57:10

标签: android android-pendingintent

我有问题。当我的应用不在后台(从最近删除)时,一切正常。但是,当我的应用程序是最近的应用程序,然后我通过通知待处理的意图打开“ ResponseActivity”时,在背面单击“ ResponseActivity”,便可以进入MainActivity(启动器活动)。 我添加了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>

1 个答案:

答案 0 :(得分:1)

如果要确保选择通知时堆栈中只有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);