https://kotlinlang.org logo
#compose
Title
# compose
r

Rizwan Minhas

09/17/2021, 9:21 AM
Hi! I am currently working on an aap with all composables (No fragment) with single activity, I am little confuse about navigation compose. If in future, I have to use fragment in this app, can I navigate from a composable to a fragment or How its work?
j

JulianK

09/17/2021, 9:49 AM
I don't think you'll ever need fragments again. Using fragments and compose together works as a migration strategy, but when you create apps from scratch, no fragments are needed. Navigation compose can't be used to navigate to fragments as far as i know.
👍 1
c

Csaba Szugyiczki

09/17/2021, 9:50 AM
What if you integrate 3rd party library that is still using Fragments?
https://developer.android.com/jetpack/compose/navigation#interoperability Im affraid you will need to include Fragment based navigation in your app, and find a way to navigate between fragments when needed, and have a ComposeFragment or such as the host of your Composable part of your application
a

Albert Chang

09/17/2021, 11:13 AM
Navigation can be used to navigate to any destination as long as there is a navigator for that type, so it's totally possible to navigate to a fragment. You can do something like this:
Copy code
val navController = rememberNavController()
val activity = LocalContext.current as FragmentActivity
val fragmentNavigator = remember(activity) {
    FragmentNavigator(activity, activity.supportFragmentManager, R.id.containter)
}
navController.navigatorProvider += fragmentNavigator
NavHost(navController, startDestination) {
    addDestination(
        FragmentNavigator.Destination(provider[FragmentNavigator::class]).apply {
            route = "SomeFragment"
            setClassName(SomeFragment::class.java.name)
        }
    )
}
😮 1
c

Csaba Szugyiczki

09/17/2021, 12:17 PM
@Albert Chang How would you set up the Activity to support having a container and your composable next to each other?
a

Albert Chang

09/17/2021, 12:19 PM
That's what you need to figure out yourself depending on your use case.
I think you can alternatively add an
ActivityNavigator
and create an activity which shows the fragment.
💯 1
i

Ian Lake

09/17/2021, 6:39 PM
Don't do any of this please. Your composable destination can inflate layouts that include fragments with
AndroidViewBinding
and they'll work just fine in Compose https://kotlinlang.slack.com/archives/CJLTWPH7S/p1621085039089800?thread_ts=1621026431.053700&cid=CJLTWPH7S
☝️ 3
😮 1
4 Views