Is it true that when using compose navigation for ...
# compose-android
m
Is it true that when using compose navigation for an Activity with
launchMode="singleTop"
, the app needs to implement
onNewIntent()
(probably using
navController.handleDeepLink(intent)
)?
m
Thanks Ian, but I want to keep as much compose code platform-neutral as possible. Can’t I just pass in (to the screen composable) a Flow of
(NavController) -> Unit
and let the Activity implement that flow?
Copy code
private val handleDeepLinkEventBus = SingleShotEventBus<(NavController) -> Unit>()
private val handleDeepLinkFlow: Flow<(NavController) -> Unit>
    get() = handleDeepLinkEventBus.events

override fun onNewIntent(intent: Intent) {
    super.onNewIntent(intent)
    setIntent(intent) // Update the intent to the new one
    handleDeepLinkEventBus.trySend { navController ->
        navController.handleDeepLink(intent)
    }
}
i
I'd think an
OnNewIntentEffect(NavController)
that is just a no-op on non-Android would be way easier to reason about, but you do you
m
I guess you’re saying that this is only applicable to Android anyway, in which case I agree. My thinking was that if this logic would be applicable to any other platform, then my approach would make sense.
i
If it applied on another platform, that effect could be...not a no-op there too. Really just depends whether you want the logic to live in composition, near your NavController, or if you want to put code in a global place like at your activity level
👍 1
m
Makes sense. Regardless, my above solution is unnecessary. As a middle ground I’m happy to keep the code in both compose and the Activity, by passing in the
navController
. It’s trivial to move that code in or out of the AppScreen composable, so a moot point IMO.
Copy code
setContent {
    val mainNavController = rememberNavController()
    DisposableEffect(Unit) {
        val listener = Consumer<Intent> { intent ->
            mainNavController.handleDeepLink(intent)
        }
        addOnNewIntentListener(listener)
        onDispose { removeOnNewIntentListener(listener) }
    }
    AppScreen(mainNavController)
}
Thanks for your quick feedback Ian. 🙏
i
Looks good to me 👍
m
Oh maybe I should add
mainNavController
as a key to the
DisposableEffect
?
i
I don't think it matters in this particular case, but yes that's generally a good habit to get into
👍 1