android - 向上导航未启动父 Activity

标签 android android-intent android-activity android-notifications

我有两个 Activity AB,其中 A 是 B 的父级。现在我显示启动 B 的通知。当我点击通知时,B 启动.然后我点击向上按钮。当 Activity A 在后台堆栈中时,它可以正常工作,否则应用程序只会关闭并且不会启动 Activity A。

我的设置。

我已经在SingleTop launchMode 中将A 声明为Manifest 中B 的父级

<activity
        android:name=".A"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:launchMode="singleTop"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="<packegeName>.Home" />
        </intent-filter>
    </activity>
    <activity
        android:name=".B"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:parentActivityName=".A"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".A" />
    </activity>

这是通知 Intent :

Intent intent = new Intent(this, B.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
                                                        PendingIntent.FLAG_ONE_SHOT);

在 Activity B 中:

@Override
public void onCreate(Bundle savedInstanceState) {
    ....
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ....
}

我尝试过的

为了调试,我尝试在 Activity B 的 onResume 中手动触发向上导航,所有这些:

1)

Intent upIntent = NavUtils.getParentActivityIntent(this);
upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
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);
}

2)

onNavigateUp();

3)

NavUtils.navigateUpFromSameTask(this);

但是当 Activity A 不在 Backstack 中时,这些似乎都不起作用,应用程序就退出了。

我搜索了 SO 和 Google(甚至是 bing!)寻找答案并尝试了我能找到的所有方法,但都无济于事。由于没有错误或日志输出,我也不知道如何调试它。

我们将不胜感激任何有关找出问题的解决方案或提示。

谢谢。

最佳答案

@pablobu 的回答对我有用,但我想对我发现的内容添加更多解释。

该死的那些文档令人困惑

我最初感到困惑的地方是由于 this :

If your activity provides any intent filters that allow other apps to start the activity, you should implement the onOptionsItemSelected() callback such that if the user presses the Up button after entering your activity from another app's task, your app starts a new task with the appropriate back stack before navigating up.

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

我的印象是这会为我生成 backstack。但有一点不同。

NavUtils.shouldUpRecreateTask(this, upIntent) 仅当当前 Activity 在另一个应用程序的任务中时才会返回 true。至于从通知启动,这将是错误的,因此任务堆栈构建代码不会执行。

正确的方法是在生成通知并将其作为未决 Intent 传递时构建后台堆栈。来自 this page :

when a notification takes the user to an activity deep in your app hierarchy, you can use this code to create a PendingIntent that starts an activity and inserts a new back stack into the target task.

Intent detailsIntent = new Intent(this, DetailsActivity.class);
// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent pendingIntent =
        TaskStackBuilder.create(this)
                        // add all of DetailsActivity's parents to the stack,
                        // followed by DetailsActivity itself
                        .addNextIntentWithParentStack(detailsIntent)
                        .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);

关于android - 向上导航未启动父 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38135453/

相关文章:

Android - loopj 异步任务取消

java - 如何获取扫描的wifi网络安全类型?在android中使用WiFi连接到特定网络所需的网络配置是什么?

android - 是否有日历提醒警报的 Intent ?

android - 无法从 Intent 中获取数据 - Android

android - 在 logcat 中显示额外的 Intent

android - 如何从 AsyncTask 使用 SQLite 数据库?

java - 从 java 到 kotlin 的 android 应用程序中的 Intent 问题

java - setMeasuredDimension() 中的尺寸不正确或尺寸计算不正确?

java - 如何将首选项值获取到静态字符串变量中?

android - Android 的 GUI-less 编程就像普通的 Linux 系统一样