1

This is my first Android app. Currently I'm working on the navigation part. Now the issue is, whenever I click the IconButton to navigate to the Add Notes Screen the app crashes with this exception:

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

In my MainActivity, I have this code:

val navController = rememberNavController()

NavHost(
    navController = navController,
    startDestination = Home,
) {
    composable<Home> {
        TopBar()
    }
    composable<AddNote> {
        // add Notes here
    }
}

And this are the navigation destinations I use:

@Serializable
object Home

@Serializable
object AddNote

This is a condensed version of my TopBar where I want to navigate to AddNote when the button is clicked:

@Composable
fun TopBar() {
    //navigate through screens
    val navController = rememberNavController()

    // IconButton to handle the add note action
    IconButton(onClick = { navController.navigate(AddNote) }) {
        Icon(
            imageVector = Icons.Rounded.Add,
            contentDescription = "Add Note",
        )
    }
}
New contributor
Shehara Nayanananda is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
3
  • Please edit your question to replace your code with a minimal reproducible example. Make sure you removed everything from the code that isn't strictly necessary to reprroduce the issue, while on the other hand making sure everything that is needed to execute it is present.
    – tyg
    Commented 12 hours ago
  • Also, in case of crashes, please always provide the stacktrace of the crash that can be found in Logcat.
    – tyg
    Commented 11 hours ago
  • I took the liberty to convert your code into a minimal reproducible example. This way the question may also be helpful for future readers having the same problem. Please do this yourself the next time you ask a question.
    – tyg
    Commented 10 hours ago

2 Answers 2

0

Your app crashes because the navController you use to navigate to AddNote is not the same as the one you used to create your NavHost.

Since the navController should never be passed around (it should be confined to the NavHost), you should instead pass a lambda to TopBar, like this:

TopBar(
    navigateToAddNote = { navController.navigate(AddNote) }
)

The new parameter is a function that can be executed by TopBar. In the function the navController is used for the navigation, but TopBar never has access to the navController itself, it can only execute the function as a whole:

@Composable
fun TopBar(
    navigateToAddNote: () -> Unit,
) {
    // IconButton to handle the add note action
    IconButton(onClick = navigateToAddNote) {
        Icon(
            imageVector = Icons.Rounded.Add,
            contentDescription = "Add Note",
        )
    }
}
1
0

Ah, I've seen this before. The reason for the crash is that you're creating a new NavController within TopBar(). It has no connection to the one from MainActivity. So when you try to navigate to AddNote, the app doesn't know where you're trying to go.

The fix is to pass the navController from MainActivity to TopBar().

Inside your TopBar() composable, do not create a new NavController, but rather accept it as a parameter:

@Composable fun TopBar(navController: NavController) {     // now you're using the navController from MainActivity }

Then in your MainActivity.kt, call the controller for TopBar() like this:

TopBar(navController = navController) 

That should do the trick! Now both MainActivity and TopBar() are using the same controller so navigating doesn't crash anymore.

New contributor
Jack K is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1
  • Please don't pass the navController to other composables, only pass lambdas. The navController shouldn't leak outside the NavHost. See my answer for an example of how it should be done instead.
    – tyg
    Commented 11 hours ago

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.