Ad

Navigating To DialogFragment Produces IllegalArgumentException If Button Pressed Quickly?

- 1 answer

My Navigation graph has two destinations, a Fragment and a DialogFragment. The Fragment contains a Button that navigates to the DialogFragment when pressed.

Everything works as expected, except if I click the button very quickly. Doing so can trigger a

IllegalArgumentException: navigation destination com.example.app:id/show_dialog is unknown to this NavController

To fix this, I ensure that the current destination is the Fragment containing the show_dialog action:

val navController = findNavController()
val currentDest = navController.currentDestination?.id

if (currentDest == R.id.test_fragment) {
    navController.navigate(TestFragmentDirections.showDialog())
}

Making this change appears to fix the issue. However, I would like to know:

Why is it necessary to wrap the navigate call with a conditional statement in this situation?

Ad

Answer

Your question is just based on the Android inside Architecture and is also dependent on the hardware performance. Just wrap it in a try/catch block:

try{
  findNavController().navigate(TestFragmentDirections.showDialog())
}catch(e: IllegalArgumentException){
    e.printStackTrace
}
Ad
source: stackoverflow.com
Ad