2014-09-18 49 views
0

我想在用户从通知中调用应用程序时恢复主要活动,但此代码正在重新创建主要活动。如何停止它?从通知调用应用程序重新创建活动

int mId=1; 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(this) 
      .setContentTitle("My notification") 
      .setAutoCancel(true) 
      .setSmallIcon(R.drawable.twitter) 
      .setContentText("Hello World!") 
      .setNumber(15); 
    Intent resultIntent = new Intent(this, MainActivity.class); 
    resultIntent.setAction(Intent.ACTION_MAIN); 
    resultIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    mBuilder.setContentIntent(resultPendingIntent); 
    NotificationManager mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(mId, mBuilder.build()); 

此外,我放置android:launchMode="singleTop"我的主要活动在我的清单file.How我能解决这个问题?

+0

[Android通知的可能重复启动同一活动两次](http://stackoverflow.com/questions/4333946/android-notification-launches-same-activity-twice) – Varun 2014-09-18 22:06:22

+0

@Varun我试过了,但没有奏效。 – 2014-09-18 22:07:36

回答

0

对于我的应用我用这个代码来做到这一点:

Intent intent = new Intent(this, MainActivity.class); 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); 

而且在清单我设置在MainActivity的午餐模式为singleTask android:launchMode="singleTask"

相关问题