IllegalArgumentException: navigation destination xxx is unknown to this NavController

后端 未结 30 2402
遇见更好的自我
遇见更好的自我 2020-11-27 11:28

I am having issue with the new Android Navigation Architecture component when I try to navigate from one Fragment to another, I get this weird error:

30条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 12:11

    Updated @Alex Nuts solution

    If there is no action for particular fragment and want to navigate to fragment

    fun NavController.navigateSafe(
    @IdRes actionId: Int, @IdRes fragmentId: Int, args: Bundle? = null,
    navOptions: NavOptions? = null, navExtras: Navigator.Extras? = null) 
    {
      if (actionId != 0) {
          val action = currentDestination?.getAction(actionId) ?: graph.getAction(actionId)
          if (action != null && currentDestination?.id != action.destinationId) {
              navigate(actionId, args, navOptions, navExtras)
        }
        } else if (fragmentId != 0 && fragmentId != currentDestination?.id)
            navigate(fragmentId, args, navOptions, navExtras)
    }
    

提交回复
热议问题