Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Child activity launched from notification does not return to parent on up navigation

I am starting an activity -- who'm has a parent activity -- from a notification in the notification drawer. The child activity has up navigation enabled. I want the child activity to return to the parent activity when the up navigation button is clicked. This behavior works for normal application flow. However, when the user enters the child activity via the notification, clicking the up navigation button does not take the user to the parent activity; instead, it ends the application.

Here is the manifest for the activity that should be launched when the notification is clicked. It has a parent TabbedActivity

<activity
    android:name=".activity.AnimalDetailsActivity"
    android:parentActivityName=".activity.TabbedActivity" >
<meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value=".activity.TabbedActivity"/>
</activity>

Here is where I generate the notification and intents:

Intent resultIntent = new Intent(context.getApplicationContext(), AnimalDetailsActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context.getApplicationContext());
stackBuilder.addParentStack(AnimalDetailsActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    .setSmallIcon(icon_resource_id)
    .setLargeIcon(large_icon)
    .setContentTitle(title)
    .setContentText(text)
    .setContentIntent(resultPendingIntent)
    .setAutoCancel(true);

return builder.build();

And then I send them off

private static void sendNotifications(List<android.app.Notification> notifications) {
  NotificationManager manager = (NotificationManager)   context.getSystemService(Context.NOTIFICATION_SERVICE);
  for (android.app.Notification notification : notifications) {
    manager.notify(++drawerId, notification);
  }
}

Inside the target activity, I enable the up button arrow and up navigation:

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  getActionBar().setHomeButtonEnabled(true);
  getActionBar().setDisplayHomeAsUpEnabled(true);

I believe I am following the guides correctly. So why is the up arrow not taking the user to the parent activity?

like image 233
Walt Dizzy Records Avatar asked Apr 21 '15 00:04

Walt Dizzy Records


2 Answers

As per the documentation, you should implement your onOptionsItemSelected() callback as follows, if you start your activity with a new back stack:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        Intent upIntent = NavUtils.getParentActivityIntent(this);
        if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
            // This activity is NOT part of this app's task, so create a new task
            // when navigating up, with a synthesized back stack.
            TaskStackBuilder.create(this)
                    // Add all of this activity's parents to the back stack
                    .addNextIntentWithParentStack(upIntent)
                    // Navigate up to the closest parent
                    .startActivities();
        } else {
            // This activity is part of this app's task, so simply
            // navigate up to the logical parent activity.
            NavUtils.navigateUpTo(this, upIntent);
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}
like image 147
Marcus Avatar answered Oct 05 '22 16:10

Marcus


To add to the accepted answer, I would add isTaskRoot() check:

if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) {

      /* ... */

}

Because when the app is launched from notification, the launched Activity IS part of the app's stack, but we still want the task recreation for parents.

like image 28
Sevastyan Savanyuk Avatar answered Oct 05 '22 14:10

Sevastyan Savanyuk