androidandroid-jetpack-composeandroid-navigation-graph

Navigation with Jetpack Compose - navigagte to another graph


I'm pretty new to the Navigation Component AND to Compose.

I have a first Activity1 (containing 3 fragments) with a Graph1:

Activity1:

setContent {
    Theme {
        Scaffold(
            topBar = { ... },
            bottomBar = { ... }
        ) { innerPadding
            Activity1NavigationGraph(navController = navController, startDestination = "home")
         }
    }
}
// graph1
@Composable
fun Activity1NavigationGraph(navController: NavHostController, startDestination: String) {
    NavHost(navController) {
        composable("home") {
            HomeScreen()
        }
        composable("lessons") {
            LessonsScreen()
        }
        composable("settings") {
            SettingsScreen()
        }
}

I have a second Activity Activity2 containing two fragments with a Graph2:

Activity2:

setContent {
    Theme {
        Scaffold(
            topBar = { ... },
            bottomBar = { ... }
        ) { ...
            Activity2NavigationGraph(navController = navController, startDestination = "list")
         }
    }
}
// graph2
@Composable
fun Activity2NavigationGraph(navController: NavHostController, startDestination: String) {
    NavHost(navController) {
        composable("list") {
            ListScreen()
        }
        composable("details") {
            DetailsScreen()
        }
    }
}

I have a button in the Fragment having with the HomeScreen Composable, when clicking on it, I want to redirect the user to the Activity2 with the Graph2.

I can use startActivity, but I want to use Navigation Component.

I saw that there are Nested Graphs, but this seems to be only to separate parts of the same graph, so that it doesn't become too complex, and not really for use another graph.

How can I achieve this?


Solution

  • I ended up doing a startActivity...

    Seems like it's the only solution