2

Upon pressing the back button, after opening the notification, the user is taken back to the Home screen instead of going back to the main page of the application. (Using a Samsung S5 with Android 5.0)

The notification is built and shown as follows:

NotificationManager mNotifyMgr =
                    (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(GcmMessageHandler.this, ListViewItemDetailActivity.class);
            Bundle b = new Bundle();
            //... put some data
            resultIntent.putExtras(b);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(GcmMessageHandler.this);
stackBuilder.addParentStack(ListViewItemDetailActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(GcmMessageHandler.this)
                            .setContentTitle("Notification")
                            .setSmallIcon(R.drawable.common_signin_btn_icon_dark)
                            .setAutoCancel(true)
                            .setDefaults(Notification.DEFAULT_ALL)
                            .setVisibility(Notification.VISIBILITY_PUBLIC)
                            .setContentIntent(resultPendingIntent)
                            .setPriority(0)
                            .setContentText(title);

mNotifyMgr.notify(mNotificationId++, mBuilder.build());

Also in the Manifest file, i have set the parentActivity as follows

<activity
        android:name=".ListViewItemDetailActivity"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
</activity>
T-D
  • 373
  • 8
  • 21

1 Answers1

3

The simplest thing that you can do is pass the bool variable form your notification e.g**(_Is_Coming_From_notification)** and in your ListViewItemDetailActivity activtiy get that variable and based on that if user go back open your app home page. below is some code for your reference.

resultIntent.putExtra("is_Comming_Form_Notification", true);

get that in your activity.

   boolean _Is_Comming_From_Notification = intent.getBooleanExtra("is_Comming_Form_Notification", false);

and in your BackPressed method

 @Override
   public void onBackPressed() {

       if (_Is_Comming_From_Notification ) {
        Intent intent = new Intent(this, App_Home_Page.class);
        startActivity(intent);
       }

       super.onBackPressed();
   }
Moubeen Farooq Khan
  • 2,875
  • 1
  • 11
  • 26
  • Well i followed the documentation https://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse and i was wondering whats the problem. Your solution works but not as desired. Wouldn't it create a new `Intent` for the App Home Page? I would prefer if the notification directly takes the user back to the already opened app in the background. – T-D Jul 16 '15 at 20:21