2016-09-23 62 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

通過這樣做,我們爲活動回溯堆棧創建了一個新任務,並且完成了舊任務。所以你的活動始終是新任務的根源。根據您的要求,這是解決方案。 –

相關問題