Home > Mobile >  Navigation Component error action not found in Android
Navigation Component error action not found in Android

Time:10-11

I'm facing a rather strange crash error:

Fatal Exception: java.lang.IllegalArgumentException: Navigation action/destination packageName.smart.view:id/action_nav_home_to_nav_screen_mirroring
cannot be found from the current destination Destination(packageName.smart.view:id/nav_screen_mirroring)
label=nav_screen_mirroring class=packageName.presentation.mirroring.MirroringFragment

This error occurs when fast click to navigate to new fragment. The crash rate is quite small, but it still happens. But I found a solution , it require check destination Id before call findNavController().navigate like this:

if (fragment.findNavController().currentDestination?.id == R.id.nav_home) {
   val action = HomeFragmentDirections.actionNavHomeToPhotoFragment()
   findNavController().navigate(action)
}

But problem is I use many navigation in project, and I don't want every time I call findNavController().navigate must check destination.

Is there any better way to fix this and avoid crash??

CodePudding user response:

fun View.clickWithDebounce(debounceTime: Long = 1000L, action: (View) -> Unit) {
    this.setOnClickListener(object : View.OnClickListener {
        private var lastClickTime: Long = 0
        override fun onClick(v: View) {
            if (SystemClock.elapsedRealtime() - lastClickTime < debounceTime) {
                return
            } else {
                action(v)
            }
            lastClickTime = SystemClock.elapsedRealtime()
        }
    })
}

in commercial projects we always use this extension for navigation

CodePudding user response:

If the navigation always happens after a click, how about make the view clickable for once only?

  • Related