Please. What is the correct way how to handle deep...
# compose-destinations
t
Please. What is the correct way how to handle deep links by
DestinationsNavHost
? I get deep link intent from the Activity and pass it to my compose app. But
DestinationsNavHost
has just
start
parameter, there is no way how to put there deep link Uri. I found that this works:
Copy code
val navController = rememberNavController()
val destinationNavigator = navController.rememberDestinationsNavigator()
DestinationsNavHost(
 ...
)
navController.handleDeepLink(deepLinkIntent)
But the issue is that it always display the
start
screen first and then replace it by the screen resolved from deeplink, which doesn't looks good. Maybe I can put some empty screen as
defaultStartDestionation
but this also looks like hack to me.
Best would be some way how to convert deeplink to the destination and then simply use it as start parameter.
r
I've registered my deeplinks using a custom scheme and then the $FULL_ROUTE_PLACEHOLDER. If you're trying to navigate from a background notification, I handle it as such:
Copy code
fun RootScreen(activity: MainActivity, viewModel: MainActivityViewModel) {
  val navController = rememberNavController()
  val destinationsNavigator = navController.rememberDestinationsNavigator()

  val destination by viewModel.deepLink.collectAsStateWithLifecycle()

  LaunchedEffect(destination) {
    destination?.let {
      destinationsNavigator.navigate(it)
      viewModel.completeDeepLink()
    }
  }

  CoreScaffold(
    navController = navController,
    topBar = { _, _ -> },
    bottomBar = { BottomBar(navController, visible = it.showsBottomBar()) },
  ) {
    DestinationsNavHost(
      navController = navController,
      navGraph = NavGraphs.root,
      modifier = Modifier.fillMaxSize(),
    )
  }
}
t
Also interesting solution.
Btw, there is bigger thread about the same topic https://kotlinlang.slack.com/archives/CJLTWPH7S/p1746130383475519