如果 activity 扩展了 AppCompatActivity,则向上按钮不起作用

Up Button not working if the activity extends AppCompatActivity

我通过 deep link 启动了 Activity B,我期待 Activity A 将在单击向上按钮时启动。

这是我的清单

<activity
    android:name=".ui.activity.B"
    android:label="@string/title_activity_search"
    android:parentActivityName=".ui.activity.A" >

    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".ui.activity.A"/>
        ...
        <!-- deep link -->
        ...     
</activity>

B Activity 代码在这里。

public class B extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
Toolbar vToolbar = ((Toolbar) findViewById(R.id.toolbar));
        setSupportActionBar(vToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

您可以 copy/past 此代码,如果 Activity B 扩展 FragmentActivity. But I need this to be working with AppCompatActivity,它就可以工作。我做错了什么?

您正在从另一个应用程序打开 Activity。所以堆栈中没有其他 Activity 可用。因此,当您按下向上按钮时,您不应该让父 Activity 出现在前面。如果目标父 activity 在任务的返回堆栈中,则将其提前。来自开发者 documentation

However, using navigateUpFromSameTask() is suitable only when your app is the owner of the current task (that is, the user began this task from your app). If that's not true and your activity was started in a task that belongs to a different app, then navigating Up should create a new task that belongs to your app, which requires that you create a new back stack.

因此,在您的情况下,您需要执行文档中所述的类似操作。

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.

You can do so by first calling shouldUpRecreateTask() to check whether the current activity instance exists in a different app's task. If it returns true, then build a new task with TaskStackBuilder. Otherwise, you can use the navigateUpFromSameTask() method as shown above.

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