2

我有一個簡單的用例。我有如下活動: A > B > CFLAG_ACTIVITY_CLEAR_TOP不能從通知部分工作

現在,我得到一個推送通知。

我做我的GCMIntentService類以下內容:

onMessage

Intent notificationIntent = new Intent(context, A.class);  
PendingIntent contentIntent = 
PendingIntent.getActivity(context,0, notificationIntent,  
PendingIntent.FLAG_CANCEL_CURRENT); 
notification.setContentIntent(contentIntent); 
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

我的期望是,活動B, C應從Activity stack被刪除,你應該重新定向到活動A

發生了什麼是,但是,A被打開;但是當你回擊按鈕時,我再次獲得C, B, A

這是什麼,我做錯了?

這裏是我試過的東西:

  1. 我已經與 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);試驗, notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP, Intent.FLAG_ACTIVITY_SINGLE_TOP);

  2. 我已經把一個靜態引用到最後前臺活動,並通過了爲context

似乎沒有任何工作。

+0

你好,你可以分享你的電子郵件ID嗎? –

+0

@ArunAntoney:[email protected] – ranjjose

回答

2

試試這個。它可能會幫助你。讓我知道一旦你測試了它。

 notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
       | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
     startActivity(notificationIntent); 
+0

感謝您的幫助,但您提供的解決方案無效。導致同樣的行爲! – ranjjose

+1

@ranjjose你可以嘗試使用這些標誌,但將它們添加到'notificationIntent'之前,你創建了'PendingIntent' – LordRaydenMK

+0

Thanks @LordRaydenMK。命令也是問題。事實上,我添加了@nun Antoney建議的'notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);並按照@LordRaydenMK的建議更改了順序。有效!感謝大家。 – ranjjose

1

試試你的活動,以 「singleTask」 這Tutorial

Intent notificationIntent = new Intent(context, YourActivity.class); 
     // set intent so it does not start a new activity 
     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
       Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = 
       PendingIntent.getActivity(context, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(context, title, message, intent); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 

     // Play default notification sound 
     notification.defaults |= Notification.DEFAULT_SOUND; 

     // Vibrate if vibrate is enabled 
     notification.defaults |= Notification.DEFAULT_VIBRATE; 
     notificationManager.notify(0, notification); 
+0

謝謝;但正如我在我已經嘗試過的部分中提到的那樣,我嘗試過'notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);'。此外,我開發了推送通知,引用同一個博客。 :) – ranjjose

+0

在我的情況下,完美的作品 – kId

+0

我們都使用相同的方法,請再次在您的應用程序中測試。 –

0

設置啓動模式:

<activity 
    android:name=".ActivityA" 
    android:launchMode="singleTask" 
> 

並設置如下標誌:

Intent intent= new Intent(currentActivity, ActivityA.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);