title: App Closes after getting redirected to an Activity When back pressed on click of push notification
body: <p>I am in big trouble and the scenario is that I have an app so when i open it up the the back stack of activities is like activity A is splash and then move to B then C and then D. So when i press back button the order of activites launch is D-C-B-A. But when a push notification is hit and after clicking on it it open Activity D. Now When I Press Back button the app closes instead the behaviour of app should be that it should redirected back to the activity (Let us say activity B )which is running at the time when notification is not clicked. 
  Thanks in advance and I would appreciate for the suggestions.</p>
  
  <p>Update Here is my Message Handler</p>
  
  <pre><code> @Override
      public void onMessageReceived(String from, Bundle data) {
          String bundledata = data.getString("data");
          String message=data.getString("message");
          sendNotification(message,bundledata);
          NOTIFICATION_ID++;
      }
  
      private void sendNotification(String message,String bundledata) {
  
          intialization();
          setNotification(message,bundledata);
  
      }
  
  
      @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
      private void setNotification(String message,String bundledata)
      {
  
          NotificationManager  mNotificationManager = (NotificationManager)
                  this.getSystemService(Context.NOTIFICATION_SERVICE);
  
  
          stackBuilder.addParentStack(GcmNotificationRedirect.class);
          Intent notificationIntent = new Intent(this, GcmNotificationRedirect.class);
          notificationIntent.putExtra("pushMessage", message);
          notificationIntent.putExtra("bundleData",bundledata);
          notificationIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
          stackBuilder.addNextIntent(notificationIntent);
  
  
          PendingIntent resultPendingIntent =
                  stackBuilder.getPendingIntent( NOTIFICATION_ID,
                          PendingIntent.FLAG_CANCEL_CURRENT
                  );
  
  
         // NotificationCompat.BigPictureStyle notiStyle = new NotificationCompat.BigPictureStyle();
          NotificationCompat.Builder mBuilder =
                  new NotificationCompat.Builder(this)
                          .setContentTitle(title)
                          .setPriority(Notification.PRIORITY_MAX)
                          .setStyle(new NotificationCompat.BigTextStyle()
                                  .bigText(message))
                          .setContentText(message)
                          .setNumber(pushNumber);
  
  
              mBuilder.setColor(this.getResources().getColor(R.color.theme_color));
              mBuilder.setSmallIcon(R.drawable.app_icon);
              mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), getNotificationIcon()));
  
  
          mBuilder.setContentIntent(resultPendingIntent);
          mBuilder.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
          Notification notification = mBuilder.build();
          notification.flags |= Notification.FLAG_AUTO_CANCEL;
          notification.defaults |= Notification.DEFAULT_SOUND;
          notification.defaults |= Notification.DEFAULT_VIBRATE;
          mNotificationManager.notify(NOTIFICATION_ID, notification);
  
      }
  </code></pre>
  
  <p>The activity which redirect message to different activity</p>
  
  <pre><code>public class GcmNotificationRedirect extends Activity {
  
      String pushMessage;
      String bundleData;
      Push_Notification_Pojo pojo;
      public static final String PRODUCT_PAGE="PRODUCT_PAGE";
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          pushRedirect();
  
      }
  
      private void pushRedirect()
      {
          pushMessage = getIntent().getStringExtra("pushMessage");
          bundleData = getIntent().getStringExtra("bundleData");
  
  /*
          Intent intent1 = new Intent(GcmNotificationRedirect.this,NavigationController.class);
          intent1.putExtra("pushMessage", pushMessage);
          intent1.putExtra("bundleData",bundleData);
          startActivity(intent1);*/
  
          parsePushNotifiactionJson(pushMessage,bundleData);
          finish();
  
      }
  
      private void parsePushNotifiactionJson(String pushMessage, String bundleJSONData){
          Intent pushNotiInetent = null;
  
          if(pushMessage!=null &amp;&amp; bundleJSONData!=null) {
              try {
  
                  JSONObject jsonObject = new JSONObject(bundleJSONData);
  
                  if (jsonObject.getString("type").equals(PRODUCT_PAGE)) {
  
                      pojo = new Push_Notification_Pojo(jsonObject.getString("productId"), jsonObject.getString("type"),pushMessage);
                      pushNotiInetent = new Intent(this, ProductDetail.class);
                      pushNotiInetent.putExtra("push_notification_pojo", pojo);
                      startActivity(pushNotiInetent);
                  }
  
                  else
                  {
                      pojo = new Push_Notification_Pojo(jsonObject.getString("promotionId"), jsonObject.getString("type"), pushMessage, true);
  
                      if(pojo!=null)
                      {
                          NavigationController.menuRequired=NavigationController.PERMOTIONAL_OFFERS;
                          pushNotiInetent = new Intent(this, NavigationController.class);
                          pushNotiInetent.putExtra("pushMessage", pushMessage);
                          pushNotiInetent.putExtra("bundleData",bundleData);
                          startActivity(pushNotiInetent);
                      }
                  }
  
                 // pushNotiInetent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
              }
              catch (JSONException e) {
                  e.printStackTrace();
              }
          }
      }
  
  
  }
  </code></pre>