2013-02-21 98 views
0

的單擊事件當我點擊通知欄,它會跳過MainActivity.class接口:通知欄

Intent notificationIntent = new Intent(context,MainActivity.class); //After click the notification, it will skip to the activity 
PendingIntent contentIntent = PendingIntent.getActivity(context,0,notificationIntent,0); 
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 

如果MainActivity.class本身已經打開了,我再次點擊通知欄,它顯示了兩個接口。誰知道如何解決這個問題?

回答

1

更改行:

Intent notificationIntent = new Intent(context,MainActivity.class); 

到:

Intent notificationIntent = 
    new Intent(context,MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); 

從Android文檔它說:

FLAG_ACTIVITY_SINGLE_TOP
如果設置,活動將不會如果它已經跑了,就會被啓動g位於歷史堆棧頂部。

,或者,如果這不是預期的行爲,檢查該標誌:

FLAG_ACTIVITY_CLEAR_TOP
如果設置,並且正在啓動已經在當前任務運行,則代替開展活動一個新的活動實例,其上的所有其他活動將被關閉,並且這個Intent將作爲新的Intent被傳遞給(現在在最上面的)舊活動。

下列文件可能是你的興趣,想了解什麼時候這些標誌將導致: