material-components-android icon indicating copy to clipboard operation
material-components-android copied to clipboard

[BottomSheetDialog] `initializeViewTreeOwners()` wasn't triggered when `set/addContentView` so `AbstractComposeView` is not supported.

Open bytebeats opened this issue 1 year ago • 2 comments
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.

bytebeats avatar Jun 25 '24 10:06 bytebeats