Reputation: 66649
Is it possible set start destination of a NavGraph
to a destination in NavGraphBuilder.navigation(){}
?
private fun NavGraphBuilder.addNavGraph(navController: NavController) {
composable<RouteA> {
RouteAScreen(navController)
}
navigation<HomeGraph>(
startDestination = RouteB
) {
composable<RouteB> {
RouteBScreen(navController)
}
composable<RouteC>(
deepLinks = listOf(
navDeepLink<RouteC>(basePath = "$uri/routeC")
)
) {
RouteCScreen(navController)
}
composable<RouteD>(
deepLinks = listOf(
navDeepLink<RouteC>(basePath = "$uri/routeD")
)
) {
RouteDScreen(navController)
}
}
}
I tested with NavHost with builder and creating graph as
@Preview
@Composable
fun NavCreateTest() {
val navController = rememberNavController()
// This NavGraph will be used with NavHost this navHostController will be assigned
val navGraph: NavGraph = remember {
navController.createGraph(
startDestination = RouteC
) {
addNavGraph(navController)
}
}
Column {
/*
NavHost(
modifier = Modifier.fillMaxSize(),
navController = navController,
graph = navGraph,
)
*/
NavHost(
modifier = Modifier.fillMaxSize(),
navController = navController,
startDestination = RouteC
) {
addNavGraph(navController)
}
}
}
If i set RouteC i get following exception
java.lang.IllegalStateException: Cannot find startDestination com.packageName.RouteC from NavGraph. Ensure the starting NavDestination was added with route from KClass.
Routes and deeplink
const val uri = "test://www.example.com"
@Serializable
object RouteA
@Serializable
object RouteB
@Serializable
object RouteC
@Serializable
object RouteD
I also tried changing destination conditionally if deeplink is not null as
val intent: Intent? = (LocalContext.current as? MainActivity)?.intent
val deeplink: Uri? = intent?.data
val isDeeplink = deeplink != null
NavHost(
modifier = Modifier.fillMaxSize(),
navController = navController,
startDestination = if (isDeeplink) RouteC else RouteA
) {
addNavGraph(navController)
}
Also didn't work if i start app via deeplink from terminal.
Upvotes: 0
Views: 72