2014-10-04 21 views
1

場景:意圖與FLAG_ACTIVITY_CLEAR_TOP不清除在頂

  1. 我從酶活性的導航 - > B.
  2. 我拉下通知抽屜,並點擊它有一個意圖開始活動的通知A.

我想要的:

  • 活動B關閉,顯示它的父級A.
  • 我點擊返回按鈕退出應用程序。
  • 實際發生的:

    活動A的新實例所示。 如果我點擊回來,A關閉,老B出現。 我必須再次點擊回到#3和#4

    我在通知的操作意圖中使用了意向標誌FLAG_ACTIVITY_CLEAR_TOP。我也聯合嘗試了FLAG_ACTIVITY_NEW_TASK和FLAG_ACTIVITY_CLEAR_TASK。我也將這兩項活動都設置爲SingleTop launchMode。

    我到底做錯了什麼?即使我使用國旗,B(頂部)爲什麼沒有被清除。

    清單與活動A和B:

    <activity 
         android:name=".A" 
         android:launchMode="singleTop"> 
         <intent-filter> 
          <action android:name="android.intent.action.MAIN" /> 
          <category android:name="android.intent.category.LAUNCHER" /> 
         </intent-filter> 
        </activity> 
    
    <activity 
        android:name=".B" 
        android:parentActivityName=".A" 
        android:launchMode="singleTop"> 
        <meta-data 
         android:name="android.support.PARENT_ACTIVITY" 
         android:value=".A" /> 
        <intent-filter> 
         <action android:name="android.intent.action.VIEW" /> 
         <category android:name="android.intent.category.DEFAULT" /> 
        </intent-filter> 
    </activity> 
    

    服務顯示通知:

    Intent contentIntent = new Intent(this,A.class); 
    contentIntent.setAction(MYACTION); 
    contentIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    PendingIntent contentPendingIntent = PendingIntent.getActivity(this, notificationId, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MyApplication.getContext()) 
        .setContentTitle("text") 
        .setContentText("text") 
        .setSmallIcon(R.drawable.icon) 
        .setContentIntent(contentPendingIntent);  
    
    NotificationManager notificationManager = (NotificationManager) MyApplication.getContext().getSystemService(Context.NOTIFICATION_SERVICE); 
           notificationManager.notify(notificationId,notificationBuilder.build()); 
    

    [編輯10月15日2014]:現在按預期工作,而完全不進行任何更改。即使我刪除了旗幟和singletop launchmode,它仍然有效。我不知道爲什麼。這很奇怪。

    在A活動
    +1

    如果您將代碼 – 2014-10-04 11:35:55

    +0

    @NadirB完成,那會更好。請現在看看。 – faizal 2014-10-04 11:46:59

    +0

    @faizal你試過我的答案嗎? – 2014-10-04 12:07:58

    回答

    0

    添加此片段

    @Override 
    public void onBackPressed() { 
    super.onBackPressed(); 
    } 
    

    如果這麼想的工作,爲你,我就另一種解決方案

    +0

    我希望能夠使用intent標誌來解決它,也就是說,我正在尋找'FLAG_ACTIVITY_CLEAR_TOP'設計的確切行爲。我不明白爲什麼它不會按照文檔的規定工作。 – faizal 2014-10-04 12:47:43

    0

    嘗試使用如下設置的標誌 -

    Intent contentIntent = new Intent(this,A.class); 
    contentIntent.setAction(MYACTION); 
    contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    contentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    
    +0

    這沒有什麼區別。 – faizal 2014-10-04 12:46:45

    相關問題