material-components-android
material-components-android copied to clipboard
[BottomSheetDialog] `initializeViewTreeOwners()` wasn't triggered when `set/addContentView` so `AbstractComposeView` is not supported.
trafficstars
Close #4207.
Because of getDelegate(): AppCompatDelegate, the calling chain of ButtomSheetDialog#add/setContentView -> AppCompatDialog#add/setContentView -> ComponentDialog#add/setContentView was broken. So initializeViewTreeOwners() wasn't triggered, then BottomSheetDialog#decorView has no LifecycleOwner. So when AbstractComposeView was inserted into the view hierarchy of BotomSheetDialog, the extension function below will return null.
fun View.findViewTreeLifecycleOwner(): LifecycleOwner? {
return generateSequence(this) { currentView ->
currentView.parent as? View
}.mapNotNull { viewParent ->
viewParent.getTag(R.id.view_tree_lifecycle_owner) as? LifecycleOwner
}.firstOrNull()
}
So the code below is triggered:
val viewTreeLifecycle =
checkNotNull(lifecycle ?: findViewTreeLifecycleOwner()?.lifecycle) {
"ViewTreeLifecycleOwner not found from $this"
}
And it's exactly what the log is mentioning.