I am using navigation component in the project which I am currently working on. The problem is that when I click two items at the same time, that causes findNavController.navigate()
to be called twice. So it throws IllegalArgumentException: navigation destination is unknown to this NavController
. I think it goes to destination, then tries to trigger second time in that destination which doesn't have target destination. Destinationception...
Is there a way to check if current fragment has given destination or not?
Right now I've fixed the problem with below code but it doesn't look right.
fun NavController.navigateSafe(direction: NavDirections) {
try {
navigate(direction)
} catch (exception: IllegalArgumentException) {
Log.e("NavigationExtensions", exception.message.orEmpty())
}
}
UPDATE
Thanks to link that @Md.Asaduzzaman shares helped me. Solution is;
fun NavController.navigateSafe(direction: NavDirections) {
currentDestination?.getAction(direction.actionId)?.let { navigate(direction) }
}