IllegalArgumentException: navigation destination xxx is unknown to this NavController

后端 未结 30 2289
遇见更好的自我
遇见更好的自我 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:06

    Today

    def navigationVersion = "2.2.1"

    The issue still exists. My approach on Kotlin is:

    // To avoid "java.lang.IllegalArgumentException: navigation destination is unknown to this NavController", se more https://stackoverflow.com/q/51060762/6352712
    fun NavController.navigateSafe(
        @IdRes destinationId: Int,
        navDirection: NavDirections,
        callBeforeNavigate: () -> Unit
    ) {
        if (currentDestination?.id == destinationId) {
            callBeforeNavigate()
            navigate(navDirection)
        }
    }
    
    fun NavController.navigateSafe(@IdRes destinationId: Int, navDirection: NavDirections) {
        if (currentDestination?.id == destinationId) {
            navigate(navDirection)
        }
    }
    

提交回复
热议问题