【问题标题】:Child activity launched from notification does not return to parent on up navigation从通知启动的子活动不会在向上导航时返回父项
【发布时间】:2018-05-17 20:32:58
【问题描述】:

我正在从通知抽屉中的通知开始一项活动——谁有一个父活动。子活动启用了向上导航。我希望子活动在单击向上导航按钮时返回到父活动。此行为适用于正常的应用程序流程。但是,当用户通过通知进入子Activity时,点击向上导航按钮并不会将用户带到父Activity;相反,它会结束应用程序。

这是单击通知时应启动的活动的清单。它有一个父 TabbedActivity

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

这里是我生成通知和意图的地方:

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();

然后我把他们送走

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);
  }
}

在目标活动中,我启用了向上按钮箭头和向上导航:

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

我相信我正确地遵循了指南。那么为什么向上箭头没有将用户带到父活动?

【问题讨论】:

    标签: android android-intent


    【解决方案1】:

    根据documentation,如果您使用新的后台堆栈开始您的活动,您应该按如下方式实现您的onOptionsItemSelected() 回调:

    @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);
    }
    

    【讨论】:

      【解决方案2】:

      要添加到已接受的答案,我会添加 isTaskRoot() 检查:

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

      因为当应用程序从通知启动时,启动的 Activity 是应用程序堆栈的一部分,但我们仍然希望为父母重新创建任务。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多