Chris Fillmore
07/12/2021, 6:49 PMcomposable(...) {
val context = LocalContext.current
SideEffect {
CustomTabsIntent.Builder().build().launchUrl(context, myUrl)
}
}
I’ll be expanding on this a bit to warm up the browser, etc, but is this basically what I need to do? Call launchUrl()
from inside the Composable?Ian Lake
07/12/2021, 7:21 PMlaunchUrl
, so it wouldn't make any sense to put that code in the same place that is calling launchUrl
Ian Lake
07/12/2021, 7:22 PMcomposable
destination for something that isn't a screen built using a composable UI is not the right approachChris Fillmore
07/12/2021, 7:24 PMNavDestination
?Ian Lake
07/12/2021, 7:25 PMlaunchUrl
method that any destination can use
• Create your own custom Navigator
for Custom tabs that has its own subclass of NavDestination
and its own NavGraphBuilder.customTab
extension method that adds that destination to your graphIan Lake
07/12/2021, 7:26 PMChris Fillmore
07/12/2021, 7:26 PMIan Lake
07/12/2021, 7:30 PMBottomSheetNavDemo.kt
file there to see what it would look like as a consumer. I'd imagine something like:
val navController = rememberNavController()
val customTabNavigator = rememberCustomTabNavigator()
SideEffect { navController.navigatorProvider += customTabNavigator }
NavHost(...) {
composable(...) {
}
customTab("about", "<http://www.example.com>") // Don't even need a lambda for the simple case
}
Ian Lake
07/12/2021, 7:33 PMActivityNavigator
and its activity
destination builder for examples of how those bits might be implemented:
• https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigati[…]untime/src/main/java/androidx/navigation/ActivityNavigator.kt
• https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:navigati[…]avigatorDestinationBuilder.kt?ss=androidx&q=ActivityNavigator
(they do a lot more than you need, given that CustomTabsIntent does so much for you)Chris Fillmore
07/12/2021, 7:34 PMChris Fillmore
07/13/2021, 3:54 PMSideEffect
when adding the Navigator to the NavController. I’ve explained here:
https://github.com/androidx/androidx/pull/173/files#r668897941Ian Lake
07/13/2021, 7:15 PMChris Fillmore
07/13/2021, 7:30 PMclass MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val navController = rememberNavController()
val customTabsNavigator = CustomTabsNavigator()
SideEffect {
navController.navigatorProvider += customTabsNavigator
}
NavHost(
navController = navController,
startDestination = "home",
) {
customTab("home")
}
}
}
}
@Navigator.Name("CustomTabsNavigator")
class CustomTabsNavigator : Navigator<CustomTabsNavigator.Destination>() {
override fun createDestination(): Destination {
return Destination(this)
}
class Destination(navigator: CustomTabsNavigator) : NavDestination(navigator)
}
fun NavGraphBuilder.customTab(
route: String,
arguments: List<NamedNavArgument> = emptyList(),
deepLinks: List<NavDeepLink> = emptyList(),
) {
addDestination(
CustomTabsNavigator.Destination(provider[CustomTabsNavigator::class]).apply {
this.route = route
arguments.forEach { (argumentName, argument) ->
addArgument(argumentName, argument)
}
deepLinks.forEach { deepLink ->
addDeepLink(deepLink)
}
}
)
}
Running this produces:
java.lang.IllegalStateException: Could not find Navigator with name "CustomTabsNavigator". You must call NavController.addNavigator() for each navigation type.
If I remove SideEffect, it runs fine.Ian Lake
07/14/2021, 4:34 AMChris Fillmore
07/14/2021, 2:55 PMChris Fillmore
07/14/2021, 2:55 PMallan.conda
07/15/2021, 9:37 AMIan Lake
07/15/2021, 1:48 PM