2014-10-04 48 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