2016-09-23 64 views
1

我试图在android中使用通知,并且我想要发生的是每当我单击通知时它将打开新活动,并会自动关闭我之前的活动。我尝试添加一些标志,但它不起作用,它一直在将活动放在另一个标志上。任何有关如何使这项工作的建议将不胜感激。如何在点击通知后关闭以前的活动并打开新的活动

这里是我的代码片段:

protected void displayNotification() { 
     Intent i = new Intent(context, Order.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

     notification = new NotificationCompat.Builder(context); 
     notification.setContentTitle("Received New Order/s"); 
     notification.setContentText("Refresh Order List"); 
     notification.setTicker("Receive New Order/s"); 
     notification.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_SOUND); 
     notification.setWhen(System.currentTimeMillis()); 
     notification.setSmallIcon(R.drawable.my_icon); 
     notification.setContentIntent(PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT)); 
     notification.setAutoCancel(true); 

     NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE); 
     nm.notify(uniqueID,notification.build()); 

    } 

回答

3

使用此:

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

看看是否有帮助

+0

它的工作。非常感谢,但我想知道这样做的缺点是什么? – newBieUser0829

+1

通过这样做,我们为活动回溯堆栈创建了一个新任务,并且完成了旧任务。所以你的活动始终是新任务的根源。根据您的要求,这是解决方案。 –

相关问题