Android: Double clicking fast on any views related to Navigation component crashes the app [duplicate]
T

1

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?

Tumulus answered 23/4, 2020 at 17:41 Comment(2)
Try read thisPodesta
#61394326Knipe
H
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
    }
}
Hilliard answered 27/8, 2020 at 3:21 Comment(1)
i guess this fix works but i wish google would fix this.Malvin

© 2022 - 2024 — McMap. All rights reserved.