1

I'm currently writing an android application and it's time to implement notifications. The three activities I will be looking at here are MainActivity, MessageListActivity and MessageActivity.

These go in a simple A > B > C format. With B's parent set as A, and C's parent set as B. The normal navigation works fine and does everything as expected. Here is the manifest below:

    <activity
        android:name=".activities.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:screenOrientation="portrait" >
    </activity>

    <activity
        android:name=".activities.MessageListActivity"
        android:icon="@drawable/ic_action_email"
        android:label="@string/title_activity_text"
        android:launchMode="singleTop"
        android:parentActivityName=".activities.MainActivity"
        android:screenOrientation="portrait" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activities.MainActivity" />
    </activity>

    <activity
        android:name=".activities.MessageActivity"
        android:icon="@drawable/ic_action_read"
        android:label="@string/title_activity_text_message"
        android:parentActivityName=".activities.MessageListActivity"
        android:screenOrientation="portrait" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activities.MessageListActivity" />
    </activity>

The problem is now I'm trying to implement notifications, if I'm out of the app it is running as a service so I can still receive messages. When a new message is received a popup is shown and I want to click it, show the MessageActivity (C), then when up or back is pressed return to MessageListActivity (B). However I open the notification and MessageActivity (C) is shown as expected, but when I press up or back it takes me to MainActivity (A). It should return to MessageListActivity (B) at this point!

To clarify :-

  • Notification appears
  • User clicks it
  • C is opened
  • User presses back or up
  • B should now open, but A opens instead.

I have created a backstack in my notification as shown below:

    Intent resultIntent = new Intent(NurseCallApplication.getContext(), MessageActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(NurseCallApplication.getContext());
    stackBuilder.addParentStack(MessageActivity.class);
    stackBuilder.addNextIntent(resultIntent);

    resultIntent.putExtra("messageObject", message);
    PendingIntent resultPendingIntent = PendingIntent.getActivity(NurseCallApplication.getContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

And in my up and back button in MessageActivity (C) I have got:

NavUtils.navigateUpFromSameTask(this);

At this point I'm really just throwing all kinds together trying to figure it out so forgive me if there are unnecessary parts in! Any help would be massivelyyy appreciated! Thanks!

Murphybro2
  • 2,207
  • 1
  • 22
  • 36

2 Answers2

0

I think your error will solve with this :

// B -> C    when user clicks any notification

stackBuilder.addParentStack(MessageListActivity.class);
Kushal
  • 8,100
  • 9
  • 63
  • 82
0

Try this

Intent resultIntent = new Intent(NurseCallApplication.getContext(), MessageActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(NurseCallApplication.getContext()); 
    stackBuilder.addParentStack(MessageActivity.class); 
    stackBuilder.addNextIntent(resultIntent);

resultIntent.putExtra("messageObject", message);            
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
Paras Dhawan
  • 364
  • 8
  • 23