FragmentContainerView and app:navGraph - This navigation graph is not referenced to any layout files

Viewed 2366

To prevent Lint warning (replace fragment with FragmentContainerView) after upgrading to latest fragments (1.2.2 version) and navigation (2.2.1 version) I replaced fragment with FragmentContainerView

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    ....
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

But after that I found error in my @navigation/nav_graph - This navigation graph is not referenced to any layout files

Everything works fine, but that error is very annoying.

4 Answers

It might be that I understand this wrong, but if everything works, this might be a classic case of Android Studio being in need of some cache clearing and restarting. Maybe even manually deleting build folders from your project.

You need to update to the latest versions of the navigation-fragment-ktx in your build.gradle(app)

def nav_version = "2.3.0-alpha03"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

Had the same "false" warning, seems it will be fixed in a future release. Meanwhile, as Android Studio suggests, the warning can be safely ignored using:

<navigation
    ...
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedNavigation" />

I have the same error however, the app compiles fine and works. I suppose we have to wait for fix in the next stable release.

Related