5

I am running Android Studio 3.6.3 and I am trying to use the new Navigation resource that is part of Android Jetpack. I did the Navigation Codelab to learn how to use this feature.

In my project, I added a Navigation resource and Android Studio automatically added the dependencies for this feature. I then created an Activity layout with a NavHostFragment in it.

However, when I go to my Navigation resource, the HOST section on the left says No NavHostFragments found.

I've tried syncing Gradle, cleaning & rebuilding, to no avail.

Interestingly, when I preview my main Activity layout, the "home destination" fragment peeks through the NavHostFragment, so it appears the relationship is established in one direction, but not the other.

How can I make my NavHostFragment appear in the Navigation resource?

Here's my layout XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="my.app.MyActivity"
    >

    <data>
        <variable
            name="viewModel"
            type="my.app.MyViewModel" />
    </data>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <fragment
                    android:id="@+id/my_nav_host"
                    android:name="androidx.navigation.fragment.NavHostFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:defaultNavHost="true"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:navGraph="@navigation/central_navigation" />
            </LinearLayout>
        </androidx.core.widget.NestedScrollView>

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways" />

        </com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.bottomappbar.BottomAppBar
            android:id="@+id/bottomAppBar"
            style="@style/Widget.MaterialComponents.BottomAppBar.Colored"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/colorPrimary" />

    </androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
Matt Mc
  • 8,882
  • 6
  • 53
  • 89

1 Answers1

2

I had the same problem. I did everything as stated in this doc: https://developer.android.com/guide/navigation/navigation-getting-started#add-navhost and it was saying "No NavHostFragments found" in my nav_graph.xml file. I restarted Android Studio and my MainActivity is now showing as a host with a NavHostFragment.

astro4
  • 317
  • 2
  • 5