Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using <FragmentContainerView> instaed of <fragment> causes the app to crash on using BottomNav and jetpack navigation [duplicate]

I'm using Android Navigation Component with bottom navigation, lint gives a warning about replacing the <fragment> tag with <FragmentContainerView> but when i replaced, findNavController is not working it gives me error about it does not have a NavController set on

Fragment

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

Activity

val navController = findNavController(R.id.nav_host_fragment)
    
    val appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.navigation_classes, R.id.navigation_schedule, R.id.navigation_settings
        )
    )
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
}
like image 239
Jimale Abdi Avatar asked Nov 15 '22 21:11

Jimale Abdi


2 Answers

As per this issue, when using FragmentContainerView, you need to find the NavController using findFragmentById() rather than using findNavController() when in onCreate():

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

This is because findNavController(R.id.nav_host_fragment) relies on the Fragment's View to already be created which isn't the case when using FragmentContainerView (as it uses a FragmentTransaction under the hood to add the NavHostFragment).

If you are using Fragment 1.4.0 or higher and View Binding, you can simply this considerably by using the getFragment() method:

val navController = binding.container.getFragment<NavHostFragment>().navController
like image 127
ianhanniballake Avatar answered Dec 16 '22 01:12

ianhanniballake


Replace this line:

NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);

with

NavController navController = getNavController();

Where getNavController() looks like this:

    // workaround for https://issuetracker.google.com/issues/142847973
    @NonNull
    private NavController getNavController() {
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
        if (!(fragment instanceof NavHostFragment)) {
            throw new IllegalStateException("Activity " + this
                    + " does not have a NavHostFragment");
        }
        return ((NavHostFragment) fragment).getNavController();
    }
like image 44
friederbluemle Avatar answered Dec 16 '22 02:12

friederbluemle