IllegalArgumentException: navigation destination xxx is unknown to this NavController

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

提交回复
热议问题