IllegalArgumentException: navigation destination xxx is unknown to this NavController

后端 未结 30 2103
遇见更好的自我
遇见更好的自我 2020-11-27 11:28

I am having issue with the new Android Navigation Architecture component when I try to navigate from one Fragment to another, I get this weird error:

相关标签:
30条回答
  • 2020-11-27 12:25

    Usually when this happens to me, I had the issue described by Charles Madere: Two navigation events triggered on the same ui, one changing the currentDestination and the other failing because the currentDestination is changed. This can happen if you double-tap, or click on two views with a click listener calling findNavController.navigate.

    So to resolve this you can either use if-checks, try-catch or if you are interested there is a findSafeNavController() which does this checks for you before navigating. It also has a lint-check to make sure you don't forget about this issue.

    GitHub

    Article detailing the issue

    0 讨论(0)
  • 2020-11-27 12:26

    I have resolved the same problem by putting check before navigate instead of boilerplate code for clicking instantly control

     if (findNavController().currentDestination?.id == R.id.currentFragment) {
            findNavController().navigate(R.id.action_current_next)}
    /* Here R.id.currentFragment is the id of current fragment in navigation graph */
    

    according to this answer

    https://stackoverflow.com/a/56168225/7055259

    0 讨论(0)
  • 2020-11-27 12:26

    I resolve this issue by checking if the next action exist in the current destination

    public static void launchFragment(BaseFragment fragment, int action) {
        if (fragment != null && NavHostFragment.findNavController(fragment).getCurrentDestination().getAction(action) != null) {       
            NavHostFragment.findNavController(fragment).navigate(action);
        }
    }
    
    public static void launchFragment(BaseFragment fragment, NavDirections directions) {
        if (fragment != null && NavHostFragment.findNavController(fragment).getCurrentDestination().getAction(directions.getActionId()) != null) {       
            NavHostFragment.findNavController(fragment).navigate(directions);
        }
    }
    

    This resolve a problem if the user click fast on 2 differents button

    0 讨论(0)
  • 2020-11-27 12:26

    It seems like you are clearing task. An app might have a one-time setup or series of login screens. These conditional screens should not be considered the starting destination of your app.

    https://developer.android.com/topic/libraries/architecture/navigation/navigation-conditional

    0 讨论(0)
  • 2020-11-27 12:26

    There could be many reasons for this problem. In my case i was using the MVVM model and i was observing a boolean for navigation when the boolean is true -> navigate else don't do anything and this was working fine but there was one mistake here

    when pressing back button from the destination fragment i was encountering the same problem .And problem was the boolean object as I forgot to change the boolean value to false this created the mess.I just created a function in viewModel to change its value to false and called it just after the findNavController()

    0 讨论(0)
  • 2020-11-27 12:26

    If you're using a recyclerview just add a click listener cooldown on your click and also in your recyclerview xml file use android:splitMotionEvents="false"

    0 讨论(0)
提交回复
热议问题