Mehdi Haghgoo
10/08/2021, 8:03 PMIan Lake
10/08/2021, 8:16 PMcomposeView.setContent
to indicate that the Fragment's content is built using Compose, but the general idea of having your screen take lambdas rather than having a direct dependency on Navigation / NavController
seems to align well with the Testing section on that same pageIan Lake
10/08/2021, 8:18 PMNavController
is owned by the NavHostFragment
that created the fragment (as per the recommendation in those first paragraphs in the Interop section); there's no NavHost
at all in the interop caseMehdi Haghgoo
10/08/2021, 8:26 PMoverride fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent{
ComposeScreen(onNavigate = { findNavController(R.id.nav).navigate(it)})
}
}
Ian Lake
10/08/2021, 8:33 PMTherefore, the recommendation for hybrid apps is to use the fragment-based Navigation component and use fragments to hold view-based screens, Compose screens, and screens that use both views and Compose. Once each screen fragment in your app is a wrapper around a composable, the next step is to tie all of those screens together with Navigation Compose and remove all of the fragments.
Ian Lake
10/08/2021, 8:33 PMMehdi Haghgoo
10/08/2021, 9:05 PMNavHost(navController, startDestination = "start"){
composable("start"){
StartScreen()
}
composable("fragment-wrapper"){
AndroidView(modifier = Modifier.fillMaxSize(),
factory = {context->
MyFragment().apply{
//...
}
}
){
}
}
}
Is this right for navigting from Compose to fragments?Ian Lake
10/08/2021, 9:12 PMNavHostFragment
and never NavHost
. That's what that paragraph says and the line right above that paragraph:
Define a navigation graph with ain Compose using Compose destinations. This is possible only if all of the screens in the navigation graph are composables.NavHost
Ian Lake
10/08/2021, 9:18 PMonClick
within the composable UI of a Fragment can bubble up that event to the Fragment itself, thus allowing you to access the NavHostFragment
owned NavController
and navigate to another fragmentMehdi Haghgoo
10/08/2021, 9:21 PMIan Lake
10/08/2021, 9:23 PMMehdi Haghgoo
10/08/2021, 9:25 PM