Hi… I have to use a legacy api which creates a fra...
# compose
m
Hi… I have to use a legacy api which creates a fragment at runtime (including some navigation data as you know it from those car navigation devices). My main app is built with compose - and the fragment is created on-demand at runtime. How can I insert this fragment into e.g. a
Card
composable?
m
Copy code
AndroidView(modifier = Modifier.padding(it), factory = { context ->
            FragmentContainerView(context).apply {
                id = fragmentId

                activity.supportFragmentManager
                    .beginTransaction()
                    .add(fragmentId, YourFragment())
                    .commit()
            }
        }, update = {
        })

        // To remove the fragment when this composable is removed.
        DisposableEffect(Unit) {
            onDispose {
                activity.supportFragmentManager
                    .findFragmentById(fragmentId)
                    ?.let { fragment ->
                        activity.supportFragmentManager
                            .commit {
                                remove(fragment)
                            }
                    }
            }
        }
    }
You can use fragment inside any composable like this