Avoiding Android navigation IllegalArgumentException in NavController
Okay, let me explain you this exception was pass because we are calling an action from a fragment(destination) which is not the current destination on the stack.
i.e you're calling an action
R.id.action_aFragment_to_bFragment
from fragmentA but in navController current destination is other that fragmentA. That's why navController through that exception:
navigation destination com.xxx.yyy:id/action_aFragment_to_bFragment is unknown to this NavController
you can check the current destination before navigate. like
Toast.makeText(context,view?.findNavController()?.currentDestination?.label,Toast.LENGTH_SHORT).show()
That will show you the current destination and i'm sure that's will be some other destination.
This will happens when we replace a fragment other than actions(like via old methods not with navigation) or we popUp that fragment before calling an action.
If that will be the case then you have to use Global Action because they don't care what current destination is.
You can use the below code before navigating to check whether the current destination is correct or not. This will make sure that the call happens from the current fragment only. This issue can be reproduced by clicking on two views simultaneously(Two items in a list).
if (findNavController().currentDestination?.id == R.id.currentFragment) {
findNavController().navigate(R.id.action_current_next)
}