https://kotlinlang.org logo
c

czuckie

10/06/2022, 10:14 PM
Has anyone had any luck embedding compose in a Flutter application? Trying to sneak in a cheeky bit in an AlertDialog via a
ComposeView()
but getting:
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.appcompat.widget.AlertDialogLayout
🤯 1
I did end up creating my own
FlutterActivityAppCompat
just incase it was some AppCompatActivity stuff that needed to be going but that didn't seem to help 😞
i

Ian Lake

10/06/2022, 11:15 PM
Keep in mind that the dialog's view is in a totally separate window from the activity's view hierarchy, so nothing in the activity level is going to help - the dialog's view needs to have a LifecycleOwner set
a

andrew

10/06/2022, 11:26 PM
I hope this is to join the compose side fully 😏
c

czuckie

10/06/2022, 11:48 PM
@Ian Lake I did not keep that in mind. How does a dialog typically get its lifecycle owner set then? For now I've thrown in the towel. FWIW: I really would like this to be compose, having done compose for a good while now, going to Flutter is like going from Kotlin to Java. Like I can see the structural similarities, but crikey, so much typing!
i

Ian Lake

10/06/2022, 11:51 PM
Compose's
Dialog
composable pulls the owners from the activity, which isn't perfect but good enough ™️ in just about every case: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]lin/androidx/compose/ui/window/AndroidDialog.android.kt;l=355
That does assume that your activity implements
ComponentActivity
or its subclasses,
FragmentActivity
or
AppCompatActivity
, which do have those owners set for you already
Which means you could set each owner just by calling
ViewTreeLifecycleOwner.set(yourDialogLayout, yourActivity)
and
yourDialogLayout.setViewTreeSavedStateRegistryOwner(yourActivity)
c

czuckie

10/07/2022, 7:48 AM
thanks for the explanation Ian! I might just give it a go and see if I can't smuggle some compose into the app that way.
99 Views