6

I have an app built with navigation component. I have a field in my graph which slide up a bottom sheet dialog fragment upon cliking on a filter icon on the toolbar. However, if i double click really fast on the toolbar filter icon or click really fast the toolbar filter icon and any other view which has navigation tied to it, my app crashes with the following error message:

java.lang.IllegalArgumentException: navigation destination com.th3pl4gu3.locky:id/action_Fragment_Card_to_Fragment_View_Card is unknown to this NavController

Below is the code example for my toolbar filter icon.

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

Is this a normal behaviour for navigation architecture component?

If not, can you please provide me a fix?

Mervin Hemaraju
  • 1,921
  • 2
  • 22
  • 71
  • 1
    Try read [this](https://stackoverflow.com/questions/51060762/illegalargumentexception-navigation-destination-xxx-is-unknown-to-this-navcontr) – sergiy tikhonov Apr 23 '20 at 20:23
  • 1
    https://stackoverflow.com/questions/61393826/delay-recyclerview-every-item-click-functionality/61394189#61394189 – MMG Apr 23 '20 at 21:55

1 Answers1

1

currentId is CardFragment in navGraph define 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
    }
}
Gavin Liu
  • 786
  • 6
  • 7