关于android:导航组件的popUpTo不删除按钮

Navigation Component's popUpTo not removing up button

我正在使用导航体系结构组件,并且具有类似于此的设置,用于在导航到特定片段时弹出堆栈:

1
2
3
4
5
<action
  android:id="@+id/navigate_to_main_screen"
  app:destination="@id/fragment_main_screen"
  app:popUpTo="@+id/navigation_main"
  app:popUpToInclusive="true"/>

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

但是,应用程序栏中的向上按钮仍然存在,单击该按钮不会执行任何预期的操作。我究竟做错了什么?为什么仍然在这里?

在主要活动中,我已经有

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

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

根据文档。

我正在使用的库版本:

1
2
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.

要解决您的问题,请替换

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

使用

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

此处有更多详细信息:AppBarConfiguration