Reputation: 24583
I would like to navigate from compose to a Fragment. I have defined a NavHost in compose:
NavHost(
navController = navController,
startDestination = DrawerScreen.Screen2.route
) {
composable(DrawerScreen.Screen1.route) {
navController.navigate(R.id.screen_one)
}
composable(DrawerScreen.Screen2.route) {
Screen2(
openDrawer = {
openDrawer()
}
)
}
}
Simple Fragment:
@AndroidEntryPoint
class ScreenOneFragment: Fragment() {
@Nullable
override fun onCreateView(
inflater: LayoutInflater,
@Nullable container: ViewGroup?,
@Nullable savedInstanceState: Bundle?
): View {
val view: View = inflater.inflate(R.layout.screen_one, container, false)
return view
}
}
However when I try to navigate I get the following exception:
java.lang.IllegalArgumentException: Navigation action/destination com.test/screenOne cannot be found from the current destination Destination(0x2b264dff) route=ScreenOne
Here is my navigation xml:
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mobile_navigation"
app:startDestination="@id/screenOne">
<fragment
android:id="@+id/screenOne"
android:name="com.test.ScreenOne"
android:label="ScreenOne" />
</navigation>
Is it possible to navigate from compose to a Fragment?
Upvotes: 9
Views: 6350
Reputation: 200130
As per the Interopability docs:
If you want to use the Navigation component with Compose, you have two options:
Define a navigation graph with the Navigation component for fragments.
Define a navigation graph with a NavHost in Compose using Compose destinations. This is possible only if all of the screens in the navigation graph are composables.
So no, you cannot navigate to a fragment destination if you are using a NavHost
(the reverse is also true: if you are using a NavHostFragment
, you can't navigate to a composable
destination: neither world knows about the other).
What it actually sounds like is that you want to add a Fragment to Compose. This would allow all of your destinations to be composable
destinations in your single NavHost
, but let one screen be implemented entirely by a fragment (your ScreenOne
).
composable(DrawerScreen.Screen1.route) {
// Assumes you have a layout with a FragmentContainerView
// that uses android:name="com.test.ScreenOneFragment"
AndroidViewBinding(ScreenOneLayoutBinding::inflate) {
val myFragment = fragmentContainerView.getFragment<ScreenOneFragment>()
// ...
}
}
Upvotes: 10