You must call setGraph() before calling getGraph()
Asked Answered
W

4

16

I have bottom navigation and replaced the tag with FragmentContainerView. It gives me an error stating that it does not have a NavController set. I posted this question on StackOverflow and fixed the problem. However, I encountered the following error when the orientation was changed and an item was selected in the bottom navigation.

java.lang.IllegalStateException: You must call setGraph() before calling getGraph()

FragmentContainer

<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" /

finding fragment

val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
    val navController = navHostFragment.navController
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    val appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.navigation_classes, R.id.navigation_schedule, R.id.navigation_settings
        )
    )
    setupActionBarWithNavController(navController, appBarConfiguration)
    navView.setupWithNavController(navController)
Wrest answered 11/12, 2019 at 17:19 Comment(0)
Z
13

This is fixed as per the 2.2.0-rc03 release notes:

NavHostFragment now correctly restores the graph after a configuration change when used with FragmentContainerView. (b/143752103)

So make sure you are using 2.2.0-rc03.

Zinciferous answered 11/12, 2019 at 18:12 Comment(1)
sir, could you please help me with this one ? I can't find a method after using 2.2.0-rc03 #60598741Extraneous
F
12

You'll get this same error if you are like me and forget to add to your FragmentContainerView

app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
Flounder answered 1/9, 2020 at 22:36 Comment(1)
this is what worked for me. the other solutions did not work.Danford
B
7

use Navigation UI's latest vesion:

// navigation
implementation "androidx.navigation:navigation-fragment:2.3.0-alpha02"
implementation "androidx.navigation:navigation-ui:2.3.0-alpha02"

it fixed on new versions

Barram answered 2/3, 2020 at 10:35 Comment(1)
add classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0-alpha02"Hangover
C
5

Follow this link to fix this issue https://developer.android.com/jetpack/androidx/releases/navigation In simpler terms upgrade your dependencies to the ones below in app module

def nav_version = "2.3.0-alpha05"

// Java

implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"

// Kotlin

implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

// Dynamic Feature Module Support

implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

// Testing Navigation

androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

in project module under dependencies if you are using safe args

  def nav_version = "2.3.0-alpha05"
    classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$nav_version"

that fixed my issue..

Crowns answered 21/4, 2020 at 5:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.