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",
)
}
}