I am currently doing code refactoring for a current Kotlin application. I would like to move to a different fragment by pressing a button from the Compose view. I know that Compose has own navigator, but can I somehow use findNavController() in Compose files? I tried sending a function to Compose files but I still get the error:
java.lang.UnsupportedOperationException: Cannot add views to ComposeView; only Compose content is supported
Current code:
Code in fragment:
binding.composeProgram.setContent {
MdcTheme {
ProgramContent(
viewModel = viewModel,
navigationController = {
findNavController().navigate(
R.id.exercise_details,
ExerciseDetailFragmentArgs(396).toBundle(),
null,
null
)
}
)
}
}
Compose file:
@Composable
fun ProgramContent(
viewModel: ProgramFragmentViewModel,
navigationController: () -> (Unit)
) {
Button(onClick = {
navigationController()
}){}
}
SOLVED: I had to add to xmlns file one line:
android:transitionGroup="true"
So in xmlns file it will looks like this:
<androidx.constraintlayout.widget.ConstraintLayout 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"
style="@style/AppTheme.Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionGroup="true"
>
<androidx.compose.ui.platform.ComposeView
android:id="@+id/compose_program"
android:layout_width="match_parent"
android:layout_height="0.dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
...