Android:快速双击与导航组件相关的任何视图会使应用程序崩溃

Mer*_*aju 6 android kotlin android-architecture-navigation android-jetpack-navigation android-bottomsheetdialog

我有一个使用导航组件构建的应用程序。我的图表中有一个字段,单击工具栏上的过滤器图标后,该字段会向上滑动底部工作表对话框片段。但是,如果我非常快速地双击工具栏过滤器图标,或者非常快速地单击工具栏过滤器图标和任何与其关联的导航的其他视图,我的应用程序将崩溃并显示以下错误消息:

java.lang.IllegalArgumentException: navigation destination com.th3pl4gu3.locky:id/action_Fragment_Card_to_Fragment_View_Card is unknown to this NavController
Run Code Online (Sandbox Code Playgroud)

下面是我的工具栏过滤器图标的代码示例。

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    return when (item.itemId) {
        R.id.Toolbar_Filter -> {
                findNavController().navigate(CardFragmentDirections.actionFragmentCardToBottomSheetFragmentCardFilter())
            true
        }
        else -> false
    }
}
Run Code Online (Sandbox Code Playgroud)

这是导航架构组件的正常行为吗?

如果没有,您能为我提供修复吗?

小智 1

currentIdCardFragment在 navGraph 中定义id

fun Fragment.findNavControllerSafety(currentId: Int): NavController? {
    try {
        val controller = NavHostFragment.findNavController(this)

        if (controller.currentDestination?.id != currentId) {
            val name = controller.currentDestination?.let {
                Utils.getApp().resources.getResourceName(it.id)
            } ?: ""
            LogLogger.i("Navigation currentDestination not match: $name")
            return null
        }
        return controller
    } catch (e: Exception) {
        return null
    }
}
Run Code Online (Sandbox Code Playgroud)