https://kotlinlang.org logo
#compose
Title
# compose
j

jitinsharma

08/12/2020, 3:16 PM
I tried
ViewGroup.setContent
extension inside a
BottomSheetDialog
and it threw this exception
Copy code
java.lang.IllegalStateException: Composed into the View which doesn't propagate ViewTreeLifecycleOwner!
Is this a known issue?
i

Ian Lake

08/12/2020, 8:04 PM
Are you using
BottomSheetDialogFragment
and overriding
onCreateView()
to create your View? Fragment, and by extension any
DialogFragment
, will have its LifecycleOwner set
But for a regular
Dialog
, that view hierarchy is in its own window and is totally uncoupled from the Activity's.
j

jitinsharma

08/13/2020, 1:54 AM
I'm doing this with
BottomSheetDialog
Copy code
private val dialog = BottomSheetDialog(context)
val layout = LayoutInflater.from(context).inflate(R.layout.filter_bottom_sheet_dialog_view, null)
dialog.setContentView(layout)
layout.findViewById<CardView>(R.id.v_card).setContent(Recomposer.current()) {
    FiltersScreen(filterState = filterState)
}
i

Ian Lake

08/13/2020, 2:58 AM
Well, besides making that forcing you to manually clean up your Dialog when the Activity does away (otherwise you'll leak your window token - this I'd something fragments does for you), then yeah, you'll need to hook up the ViewTreeLifecycleOwner yourself
That's what it's
set
method is for. You'll also want to hook up the other ViewTree APIs
j

jitinsharma

08/13/2020, 3:02 AM
Thanks, will look into
set
methods.
BottomSheetDialogFragment
looks an obvious better choice then(funny didn't think of this earlier 😅)
l

louiscad

11/27/2022, 11:31 AM
You'll also want to hook up the other ViewTree APIs
What did you mean?
v

Vinay Gaba

12/02/2022, 10:49 PM
There's some inspiration here as well. Different issue but needed some of the same setup - https://www.jetpackcompose.app/snippets/AbstractComposeviewPreviewHelper
7 Views