I'm using Android navigation jetpack library with BottomNavigationView. I have implemented the NavHost, the NavGraph and my fragments. Everything is working as intented when I use actions to navigate.

我使用以下代码设置所有内容:

 val navController = Navigation.findNavController(this, R.id.nav_host)
 bottom_navigation.setupWithNavController(navController)

问题是,如果我点击一个标签2次,片段就会被重新创建两次.有没有办法拦截导航?我不想导航到正在显示的同一个片段.

推荐答案

As per this issue,

请随意设置OnNavigationItemReselectedListener,它优先于NavigationUI设置的OnNavigationItemSelectedListener.

val navController = Navigation.findNavController(this, R.id.nav_host)
bottom_navigation.setupWithNavController(navController)
bottom_navigation.setOnNavigationItemReselectedListener {
  // Do nothing to ignore the reselection
}

Kotlin相关问答推荐

如何在 Big Data 中使用Inc过滤器?

Kotlin-stdlib中的模拟扩展函数

"Kotlin中的表达式

有没有一种简单的方法来识别物体?

如何在操作系统版本上正确获取Room数据库的路径>;=26 sdk?

垂直滚动条下拉菜单的桌面组合

Kotlin 基于参数类型的返回类型推断

修改器的属性是什么,我需要更改以使角变圆且宽度更小?喷气背包组合

循环中的每个元素都应填充行中可用的所有可用空间

将 Integer 转换为 Unit 编译成功

Kotlin SAM/功能接口抛出 AbstractMethodError

如何使用 gradle 脚本 Kotlin 构建文件构建可运行的 ShadowJar?

Kotlin-Java 互操作不能与可变参数一起使用

什么是开放式property?为什么我不能将其设置器设为private私有?

如何将vararg转换为list?

Kotlin解构when/if语句

Android Kotlin 创建类实现 Parcelable 在 writeToParcel 方法的 override中给出错误

无法解决:androidx.lifecycle:lifecycle-extensions-ktx:2.0.0-alpha1

可以在函数参数中使用解构吗?

将字符串编码为Kotlin中的UTF-8