Hello, can I combine fragment navigation with comp...
# compose
d
Hello, can I combine fragment navigation with compose navigation in android? I saw that the builder has these methods:
composable
and
fragment
Can I use both to make my app use compose navigation while having support for existing fragments?
j
Its possible but dont recommend it 🙂 Also feels like #compose-android but want to share code with other platforms as well?
d
I don't want to share code with other platforms, only android
Sorry, I asked in the wrong place
j
I think its possible to share same NavController as compose using same as androidx navigation for regular views. I havent mixed NavHost and Fragment Host View thing myself, so not sure how behaves. But if just having one fragment and using navigation inside it works. More complex if mixing nested navigation between compose and fragments however.
The main concept would be:
Copy code
override fun onCreateView( /* ... */ ) {
    setContent {
        MyScreen(onNavigate = { dest -> findNavController().navigate(dest) })
    }
}
d
Yeah, I was trying to combine both and not use fragments for compostable entries
Copy code
@Composable
fun MyNav(fragmentNavigator: FragmentNavigator) {
  val navController = rememberNavController(fragmentNavigator)
  NavHost(navController, "1") {
    composable("1") {...}
    fragment<MyFragment>("2")
  }
}
Tried it without passing the fragment navigator but it failed, now I don't know how to create a fragment navigator to be used in this case.
j
Oh Fragment inside compose not sure if possible in that direction. I would guess it works for dialogs or activity however 🙂
I would guess need some layer back to host Activity creating the Fragment with FragmentManager and inflate back to compose navigation, but not recommend it. Maybe exists any util, but none I am aware of.
d
Ok, thanks!