Rizwan Minhas

    Rizwan Minhas

    1 year ago
    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?
    JulianK

    JulianK

    1 year ago
    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.
    Csaba Szugyiczki

    Csaba Szugyiczki

    1 year ago
    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
    Albert Chang

    Albert Chang

    1 year ago
    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:
    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)
            }
        )
    }
    Csaba Szugyiczki

    Csaba Szugyiczki

    1 year ago
    @Albert Chang How would you set up the Activity to support having a container and your composable next to each other?
    Albert Chang

    Albert Chang

    1 year ago
    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.
    i

    Ian Lake

    1 year ago
    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 Composehttps://kotlinlang.slack.com/archives/CJLTWPH7S/p1621085039089800?thread_ts=1621026431.053700&cid=CJLTWPH7S