如何避免 "navigation XYZ cannot be found from the current destination"
How to avoid "navigation XYZ cannot be found from the current destination"
我的 Firebase 的 crashlytics 出现了一些崩溃,但有一个例外
Fatal Exception: java.lang.IllegalArgumentException Navigation ACTION_XYZ cannot be found from the current destination
我调试了这个案例,发现了问题所在:
我有一个从片段 A 导航到片段 B 的按钮,效果很好。
但是当你快速点击按钮两次时 - 首先它正确导航,然后再次尝试导航,因此出现异常。
应该如何避免这样的错误?我可以默默地捕捉到按钮点击的异常,但对我来说这看起来像是代码的味道。
我也可以在第一次点击后禁用该按钮,但我想知道是否有更优雅、更简洁的方法来避免双重导航到 FragmentB?
感谢大家的回答,干杯!
在导航到任何屏幕之前,您可以检查我们是否有从当前屏幕到该特定目的地的操作。像下面这样的东西。您可以使用此扩展功能安全地导航到任何目的地。
fun NavController.navigateSafe(directions: NavDirections, navOptions: NavOptions? = null) {
currentDestination?.getAction(directions.actionId)?.let {
navigate(directions, navOptions)
}
}
我的 Firebase 的 crashlytics 出现了一些崩溃,但有一个例外
Fatal Exception: java.lang.IllegalArgumentException Navigation ACTION_XYZ cannot be found from the current destination
我调试了这个案例,发现了问题所在: 我有一个从片段 A 导航到片段 B 的按钮,效果很好。 但是当你快速点击按钮两次时 - 首先它正确导航,然后再次尝试导航,因此出现异常。
应该如何避免这样的错误?我可以默默地捕捉到按钮点击的异常,但对我来说这看起来像是代码的味道。 我也可以在第一次点击后禁用该按钮,但我想知道是否有更优雅、更简洁的方法来避免双重导航到 FragmentB?
感谢大家的回答,干杯!
在导航到任何屏幕之前,您可以检查我们是否有从当前屏幕到该特定目的地的操作。像下面这样的东西。您可以使用此扩展功能安全地导航到任何目的地。
fun NavController.navigateSafe(directions: NavDirections, navOptions: NavOptions? = null) {
currentDestination?.getAction(directions.actionId)?.let {
navigate(directions, navOptions)
}
}