PendingIntent取消打开的Activity

时间:2014-09-09 02:16:46

标签: android

我有两个活动,活动1是列表视图,当您点击其中一个项目时,它将打开活动2.当活动1 活动时,我通过动态广播使用通知接收器,因此当活动1未激活且活动2处于活动状态时,用户将收到通知。现在我的问题是当用户处于活动2并且他获得通知并点击它时,将打开一个新的活动1,我真正需要的是关闭活动2并打开活动1,你能告诉我怎么能我这样做了?

private void prepNotification(Context context, String msg) {

    Resources r = context.getResources();
    PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context,
            TestNotification.class),  
       PendingIntent.FLAG_UPDATE_CURRENT/*I tried many other options none of which seems to work */);

    Notification notification = new NotificationCompat.Builder(context)
            .setTicker("New Notif").setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("New Notif").setContentText(msg)
            .setContentIntent(pi).setAutoCancel(true).build();

    showBackgroundNotification(context, 0, notification);

}

private void showBackgroundNotification(Context context, int requestCode, Notification notification) {
    Intent i = new Intent(ACTION_SHOW_NOTIFICATION);
    i.putExtra("REQUEST_CODE", requestCode);
    i.putExtra("NOTIFICATION", notification);

    context.sendOrderedBroadcast(i, PERM_PRIVATE, null, null, Activity.RESULT_OK,
            null, null);
}

编辑1

这两个活动都是托管片段,但不确定这是否重要。

1 个答案:

答案 0 :(得分:0)

Add this intent flag.



 Intent a = new Intent(context,
                TestNotification.class);
            a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     a.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

PendingIntent pi = PendingIntent.getActivity(context, 0, a),