2014-08-28 25 views
0

我遇到了通知意圖問題。我有一個服務(服務檢查消息並創建通知),它會創建通知。應用程序有一個操作欄,通過幻燈片菜單,用戶可以在活動之間導航。Android通知在現有活動上打開

http://i.stack.imgur.com/YJXe6.png

當用戶單擊通知它打開在當前活動的新活動。(像一個獨立的新實例)。我想在同一個實例中打開它們,就好像用戶手動導航(如單擊甲對乙的活動即當)

我現在的活動launchMode是標準的(雖然我試過singleTop和singleTask和標誌)

當前的通知代碼:

Intent i = null; 
i = new Intent(this, MessagesListActivity.class); 
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,i, 0); 
Builder notificationBuilder = new NotificationCompat.Builder(this); 
notificationBuilder.setContentTitle(title); 
notificationBuilder.setContentText(msg); 
notificationBuilder.setSmallIcon(R.drawable.speech_bubble_orange); 
notificationBuilder.setContentIntent(contentIntent); 
notificationBuilder.setAutoCancel(true); 
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
Notification notification = notificationBuilder.build(); 
notificationManager.notify(Constants.UNREADMESSAGESNOTIFICATIONID,notification); 

感謝您的幫助。

回答

1

我解決了這個問題。通過這種組合,新推出的活動清除了棧回。我也沒有改變launchmodes(仍然是標準的)

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
0

在你的清單,你必須標記爲單個實例的活動,並保持singleTask

android:launchMode= "singleTask" | "singleInstance" 

此外,你可能需要從你的意圖刪除單個頂部標誌。

+0

試過了,不工作它仍然從當前應用程序單獨開始活動。順便說一句,在導航活動之間我殺了以前的活動,因爲他們也打開當前活動的默認(改變singleTask /實例不會改變這種行爲)。我正在考慮一種解決方法,例如殺死通知點擊上的活動。不管怎麼說,還是要謝謝你 – Warwicky 2014-08-29 01:57:50

0

如果我理解正確,我認爲你要找的是TaskStackBuilder。它允許您創建一個後臺堆棧,以便爲PendingIntent正在啓動的Activity提供正確的導航。

有關更多信息,請參閱文檔here