2017-02-28 52 views
0

當用戶點擊通知時,它會打開新的活動工作正常。但是當我按下後退按鈕它關閉應用程序stackBuilder.addParentStack不工作 - 導航到主要活動(OnBackPressed)

我想要什麼? 當我點擊返回按鈕時,它返回MainActivity(Selected Activity)Everytim。

private void generateNotificationNew(Context context, String message,String pushIdmessage) { 

     Intent resultIntent = null; 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
         .setSmallIcon(R.mipmap.ic_launcher) 
         .setContentTitle(context.getString(R.string.app_name)) 
         .setAutoCancel(true) 
         .setContentText(message); 



     resultIntent = new Intent(context,ResultActivity.class); 
     //Intent resultIntent = new Intent(this, AvailableJobActivity.class); 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     // Adds the back stack for the Intent (but not the Intent itself) 
     stackBuilder.addParentStack(MainActivity.class); 
     // Adds the Intent that starts the Activity to the top of the stack 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = 
       stackBuilder.getPendingIntent(
         5, 
         PendingIntent.FLAG_UPDATE_CURRENT 
       ); 
     mBuilder.setContentIntent(resultPendingIntent); 
     NotificationManager mNotificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     // mId allows you to update the notification later on. 
     mNotificationManager.notify(0, mBuilder.build()); 
    } 

stackBuilder.addParentStackstackBuilder.addNextIntent可能無法正常工作。 任何替代選項謝謝。

回答

0

對於生成通知你需要把下面的代碼...

NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("Notificatin Title") 
       .setContentText("message"); 
Intent notificationIntent = new Intent(this , ResultActivity); 
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
stackBuilder.addNextIntent(notificationIntent); 
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0 , PendingIntent.FLAG_UPDATE_CURRENT); 
builder.setContentIntent(pendingIntent); 
builder.setAutoCancel(true); 
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
notificationManager.notify(0 , builder.build()); 

而對於處理回事件中,你需要檢查以下事項onBackPressed()結果活動

@Override 
public void onBackPressed() { 
    if(this.isTaskRoot()) 
     startActivity(new Intent(this , MainActivity.class)); 
    super.onBackPressed(); 
} 

現在您可以從通知開始活動重定向到MainActivity .. 享受它 ... :-)

+1

Thaks你,它解決了我的疑問:) –