2014-09-18 47 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"

相關問題