在未启动的情况下转到父级 activity

Go to parent activity without it being started

我正在尝试实现 "go back" 到 root activity 的功能。

我有我的 ActionBar 集

setDisplayHomeAsUpEnabled(true);

在我的清单中我设置了

<activity
        android:name=".Activity2"
        android:label="@string/title_activity2"
        android:parentActivityName=".Activity1">
        <meta-data android:name="android.support.PARENT_ACTIVITY"
            android:value=".Activity1"/>
</activity>

如果我将应用程序启动到 Activity1 并启动 Activity2,则会出现左上角的后退箭头并将我带回 Activity1。

问题是,通过通知我可以直接启动 Activity2,当我尝试使用后退按钮时,我只是回到主屏幕。

所以我搜索了这个问题并找到了 (http://developer.android.com/training/implementing-navigation/ancestral.html)

在onOptionsItemSelected中说要实现的地方

 public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    Log.d("Writing Activity","OnOptionsItemSelected entered");
    int id = item.getItemId();
    boolean handled = true;
    Log.d("Writing Activity","OnOptionsItemSelected id: "+id);
    switch (id) {
        case R.id.action_save:
            saveFile();
            break;
        case R.id.action_delete:
            deleteFile();
            break;
        case R.id.action_help:
            help();
            break;
        case R.id.up:
        case R.id.home:

            Log.d("Writing Activity","OnOptionsItemSelected Home");
            Intent upIntent = NavUtils.getParentActivityIntent(this);
            if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                Log.d("Writing Activity","OnOptionsItemSelected 1");
                // 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 {
                Log.d("Writing Activity","OnOptionsItemSelected 2");
                // This activity is part of this app's task, so simply
                // navigate up to the logical parent activity.
                NavUtils.navigateUpTo(this, upIntent);
            }
            break;
        default:
            Log.d("Writing Activity","OnOptionsItemSelected default");
            handled = super.onOptionsItemSelected(item);
    }


    return handled;
}

这对我来说似乎合乎逻辑,但没有成功。

有什么我遗漏的吗?

更新:我记录了这个方法,它似乎根本没有进入案例 R.id.home 并转到默认语句。但是当我捕捉到所选项目的 ID 时,我得到对应于家的 16908332 ......? –

好吧,这不完全是问题的答案,而是使它按照我想要的方式工作的解决方案。

在调用 setContentView 方法之前,我启动了 Activity1 并在意图中使用额外参数启动了 Activity2,而不是将其直接从通知路由到 Activity2。 这让它看起来像是直接启动到 Activity2。

然后我还必须删除额外的意图参数。 如果我不这样做,那么如果我稍后从应用程序历史记录中再次打开该应用程序,旧的意图将保留并重新运行。

好吧,似乎上述问题的真正答案是 switch case 不是因为我使用的是 R.id.home 而不是 android.R.id.home.

如果我更改为它,它会按下开关,作业可以从那里继续。