IllegalArgumentException: navigation destination xxx is unknown to this NavController

后端 未结 30 2401
遇见更好的自我
遇见更好的自我 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:17

    I got this same error because I used a Navigation Drawer and getSupportFragmentManager().beginTransaction().replace( ) at the same time somewhere in my code .

    I got rid of the error by using this condition(testing if the destination) :

    if (Navigation.findNavController(v).getCurrentDestination().getId() == R.id.your_destination_fragment_id)
    Navigation.findNavController(v).navigate(R.id.your_action);
    

    In my case the previous error was triggered when I was clicking on the navigation drawer options. Basically the code above did hide the error , because in my code somewhere I used navigation using getSupportFragmentManager().beginTransaction().replace( ) The condition -

     if (Navigation.findNavController(v).getCurrentDestination().getId() ==
      R.id.your_destination_fragment_id) 
    

    was never reached because (Navigation.findNavController(v).getCurrentDestination().getId() was always poiting to home fragment. You must only use Navigation.findNavController(v).navigate(R.id.your_action) or nav graph controller functions for all your navigation actions.

提交回复
热议问题