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

标签 android android-layout android-fragments android-architecture-navigation

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

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

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

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

在我已经有的主要 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

关于android - 导航组件的 popUpTo 不删除向上按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54003666/

相关文章:

android - 从脚本加载不同的查看器配置文件

Android谷歌加登录按钮样式

android - 升级到 Android Studio Artic Fox 和 AGP 7.0.0 后,自定义 View 不再在 Layout Preview 中呈现

android - 强制编辑文本在 ScrollView 中占据全屏

Android ConstraintLayout - 两个布局的顶部约束

Android - 使用 fragment 控制我的选项卡布局中每个选项卡的内容

java - 如何从另一个 Activity 打开或启动 fragment ?

Android 按钮文字颜色始终为粉红色

android - iOS ARCore - 如何使用 ARCore 连接 Android 和 iOS 设备并发送和接收 AR 对象?

android - onBackPressed中需要popBackStack