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

Bradleycorn

03/14/2022, 11:23 PM
I’m using Navigation Compose and some of the destinations in my graph use
AndroidViewBinding
to load a
FragmentContainerView
with “legacy” fragments (until we can get them converted to compose):
Copy code
composable(route="my-screen") { AndroidViewBinding(MyFragmentBinding::inflate) }
What I’m struggling with is … when some action is taken in the Fragment (like say a button is clicked), I need to navigate to a new destination (route). How can I do that?
In the fragment
findNavController()
doesn’t work since the NavController is defined in Compose:
val navController = rememberNavController()
Do I need to provide the navcontroller to the container Activity somehow? Do I need to provide a callback interface that I can give to the Fragment that I can use to navigate?
Perhaps I could do:
Copy code
AndroidViewBinding(MyFragmentBinding::inflate) {
    Navigation.setViewNavController(myFragmentContainer, navController)
}
Trying that …
👍🏼 1
That works .. is this the right way to do it?
i

Ian Lake

03/14/2022, 11:55 PM
I'm not mad with that solution
👍🏼 2
👍 3
We generally recommend the other way around: keep using Navigation with Fragments until every fragment is converted to Compose, then swap over to Navigation Compose and drop fragments entirely: https://developer.android.com/jetpack/compose/navigation#interoperability
But the general rule of 'interop works both ways' is true here as well if you find it works
b

Bradleycorn

03/15/2022, 3:36 PM
Thanks @Ian Lake! … That’s what I’m actually working on. We’re (slowly) converting an app to compose, and we still have a lot of “legacy” Fragments. Meanwhile our product team wants to migrate from a Drawer for navigation to a Bottom Nav Bar. Since it’s so heavily tied to navigation, I thought I would explore the idea of going ahead and migrating the navigation and core ui over to compose (i.e a Scaffold with a BottomNavBar and NavHost) and use
AndroidViewBinding
to load in the “legacy” Fragments until they can be converted. It definitely seems plausible, though I’d still have to make updates in the
Fragments
(for example, where they use
navArgs
to get their arguments bundle)
🙌 1
16 Views