Reputation: 11100
I add ComposeView
straight to activity root (this is how I launch ModalBottomsheetLayout
from xml view, example below is simplified but still reproducible)
Here is the whole fragment of reproducible example:
class TestFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(
ViewCompositionStrategy
.DisposeOnViewTreeLifecycleDestroyed,
)
setContent {
LazyColumn(
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.CenterHorizontally,
) {
item {
ListItem(title = "launchBS") { launchBS() }
}
item {
ListItem(title = "closeBS") {
activity
?.findViewById<ViewGroup>(android.R.id.content)
?.let { viewGroup ->
val viewToRemove = viewGroup.findViewWithTag<ComposeView>(BS_VIEW_TAG)
viewToRemove?.disposeComposition()
viewGroup.removeView(viewToRemove)
}
}
}
}
}
}
}
private fun launchBS() {
activity
?.findViewById<ViewGroup>(android.R.id.content)
?.let { viewGroup ->
val composeView = ComposeView(viewGroup.context).apply {
setViewCompositionStrategy(
ViewCompositionStrategy.DisposeOnDetachedFromWindow
)
setContent {
val navController = rememberNavController()
NavHost(navController, startDestination = DEFAULT_DESTINATION) {
composable(DEFAULT_DESTINATION) {
Text(text = "COMPOSE BOTTOMSHEET IS DISPLAYING")
}
}
DisposableEffect(Unit) {
onDispose {
navController.popBackStack(DEFAULT_DESTINATION, true)
}
}
}
tag = BS_VIEW_TAG
}
viewGroup.addView(
composeView
)
}
}
}
Explanation:
The problem:
Questions:
Here is the reproducible example https://github.com/dkachan1941/Android-NavHost-Leak-Demo
Upvotes: 2
Views: 174