Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Resource displays No NavHostFragments Found

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>
like image 259
Matt Mc Avatar asked Oct 26 '22 22:10

Matt Mc


People also ask

How do I get NavController in fragment?

To retrieve the NavController for a fragment, activity, or view, use one of the following methods: Kotlin: Fragment. findNavController()

What is the resource type of navigation graph?

A navigation graph is a resource file that contains all of your app's destinations along with the logical connections, or actions, that users can take to navigate from one destination to another. You can manage your app's navigation graph using the Navigation Editor in Android Studio.


1 Answers

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.

like image 160
astro4 Avatar answered Dec 16 '22 01:12

astro4