导航组件的 popUpTo 不删除向上按钮

Navigation Component's popUpTo not removing up button

我正在使用导航架构组件,并且我有一个类似于 的设置,用于在导航到特定片段时弹出堆栈:

<action
  android:id="@+id/navigate_to_main_screen"
  app:destination="@id/fragment_main_screen"
  app:popUpTo="@+id/navigation_main"
  app:popUpToInclusive="true"/>

这几乎符合预期。系统后退按钮和应用栏中的向上图标都不会导航到上一个片段。系统后退按钮退出应用程序。

但是,应用栏中的向上按钮仍然存在,点击它没有按预期执行任何操作。我究竟做错了什么?为什么它还在这里?

主要activity我已经有

AppBarConfiguration config =
    new AppBarConfiguration.Builder(navController.getGraph()).build();
NavigationUI.setupActionBarWithNavController(this, navController, config);

@Override
public boolean onSupportNavigateUp() {
  return navController.navigateUp() || super.onSupportNavigateUp();
}

根据 documentation.

我使用的库版本:

implementation 'android.arch.navigation:navigation-fragment:1.0.0-alpha09'
implementation 'android.arch.navigation:navigation-ui:1.0.0-alpha09'

If you want to customize which destinations are considered top-level destinations, you can instead pass a set of destination IDs to the constructor, as shown below.

要解决您的问题,请替换

AppBarConfiguration config =
    new AppBarConfiguration.Builder(navController.getGraph()).build();

AppBarConfiguration config =
        new AppBarConfiguration.Builder(R.id.navigation_main, R.id.fragment_main_screen).build();

此处有更多详细信息:AppBarConfiguration