Using FragmentContainerView with Navigation component?

AndroidAndroid Layout

Android Problem Overview


After updating to Navigation 2.2.0-beta01 from the previous version, lint gives a warning about replacing the <fragment> tag with FragmentContainerView.

However, replacing the tag alone seems to prevent the navigation graph from being inflated.

According to 2.2.0-alpha01, FragmentContainerView is used internally. Should we ignore the lint warning?


activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Lint warning: "Replace the <fragment> tag with FragmentContainerView. -->
    <fragment
        android:id="@+id/nav_host_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph_main"/>

    <!-- other elements -->

</androidx.constraintlayout.widget.ConstraintLayout>

Android Solutions


Solution 1 - Android

There is still a bug that will thrown an exception if you simply replace fragment for . The fix, as of now, as stated by many google enginners in this bug thread is to change your code in your activity:

val navHostFragment = supportFragmentManager.findFragmentById(R.id.my_nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController

Solution 2 - Android

Important! There are known issues with different versions of FragmentContainerView. See the changelog before using. Read through bug fixes and use a recent version of the library.


For now, you should also declare dependency on Fragment 1.2.0-beta02 as it includes a fix for this use case.

implementation "androidx.fragment:fragment:1.2.0-beta02"

> ### Version 1.2.0-beta02 > October 11, 2019 > > Bug fixes > > Fixed an issue where Fragment's onInflate() did not receive proper attributes from FragmentContainerView, breaking cases such as NavHostFragment. (b/142421837)

Source: https://developer.android.com/jetpack/androidx/releases/fragment#1.2.0-beta02

Solution 3 - Android

The NavigationBasicSample has been updated to 2.2.0-alpha01 but is still using the fragment tag. The NavigationAdvancedSample shows the use of the FragmentContainerView, but the nav graph is inflated in the code (they have several different graphs) and the corresponding host fragment is added to the FragmentContainerView. So I'd say at this time we should ignore the warning if we want automatic inflation.

Solution 4 - Android

using Java

NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager()
                .findFragmentById(R.id.nav_host_fragment);
navController = navHostFragment.getNavController();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser12194200View Question on Stackoverflow
Solution 1 - AndroidHenrique VasconcellosView Answer on Stackoverflow
Solution 2 - AndroidEugen PechanecView Answer on Stackoverflow
Solution 3 - AndroiddmaprView Answer on Stackoverflow
Solution 4 - AndroidMeysam KeshvariView Answer on Stackoverflow