Android Navigation component: No NavHostFragments found error

I had the same issue and I solved this by creating the NavHostFragment from the Design, Palette and then assign the respective navigation resource file.


As mentioned in Doc, You can also use the Layout Editor to add a NavHostFragment.

  1. In your list of project files, double-click on your activity's layout XML file to open it in the Layout Editor.
  2. Within the Palette pane, choose the Containers category, or alternatively search for "NavHostFragment".
  3. Drag the NavHostFragment view onto your activity.
  4. Next, in the Navigation Graphs dialog that appears, choose the corresponding navigation graph to associate with this NavHostFragment, and then click OK.

YOU MAY NEED TO RESTART THE ANDROID STUDIO AFTER THIS (not mentioned in the doc)

Also, you may see "fragment" instead of "FragmentContainerView" when you try to add fragment using above method.


The way I was able to resolve the issue was by going to the Activity.xml and changing the fragment class/component tag from androidx.fragment.app.FragmentContainerView to just fragment. It then showed up in the hosts panel of the navigation graph.