Hello all, I just filed this bug (<https://issuetracker.google.com/issues/261078350>) regarding a cr...
z
Hello all, I just filed this bug (https://issuetracker.google.com/issues/261078350) regarding a crash that occurs when placing an
AbstractComposeView
inside of a
BottomSheetDialog
(from the material design library). Wondering if anyone else has run into this before & found a solution. Error starts with:
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from android.widget.FrameLayout
Copy code
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from android.widget.FrameLayout{7ec5e5f V.E...... ......I. 0,0-0,0 #7f08006f app:id/container}
                                                                                                        at androidx.compose.ui.platform.WindowRecomposer_androidKt.createLifecycleAwareWindowRecomposer(WindowRecomposer.android.kt:349)
                                                                                                        at androidx.compose.ui.platform.WindowRecomposer_androidKt.createLifecycleAwareWindowRecomposer$default(WindowRecomposer.android.kt:324)
                                                                                                        at androidx.compose.ui.platform.WindowRecomposerFactory$Companion$LifecycleAware$1.createRecomposer(WindowRecomposer.android.kt:168)
                                                                                                        at androidx.compose.ui.platform.WindowRecomposerPolicy.createAndInstallWindowRecomposer$ui_release(WindowRecomposer.android.kt:224)
                                                                                                        at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:299)
                                                                                                        at androidx.compose.ui.platform.AbstractComposeView.resolveParentCompositionContext(ComposeView.android.kt:240)
                                                                                                        at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:247)
                                                                                                        at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.android.kt:279)
                                                                                                        at android.view.View.dispatchAttachedToWindow(View.java:20479)
i
See the last thread about directly using a
BottomSheetDialog
(when really you'd run into less problems if you use
BottomSheetDialogFragment
): https://kotlinlang.slack.com/archives/CJLTWPH7S/p1597245390048100
This isn't unique to
BottomSheetDialog
, you see the same thing in every non-managed
Dialog
that doesn't know anything about Compose
z
Got it, thanks for the link! If I wanted to avoid using the dialog fragment, I have to manage the ViewTreeLifecyclerOwner myself? I gave that a shot but it didn’t seem to have any effect
i
You need to create your own LifecycleOwner that matches the Lifecycle of the Dialog. Same for a
SavedStateRegistryOwner
(and optionally a
ViewModelStoreOwner
if you want to use ViewModels within the dialog)
These are some of the many things that Fragments do for you in this case
z
got it, we don’t use any fragments in the app so I’d need to refactor how we display bottom sheets.
we have one activity & everything is a View. using https://github.com/Zhuinden/simple-stack
i
If you upgrade to AppCompat 1.5.0, then
AppCompatDialog
(the superclass of
BottomSheetDialog
) will extend
ComponentDialog
, which will give you the
LifecycleOwner
part for free
I filed https://issuetracker.google.com/issues/261162296 to see if
ComponentDialog
can also implement
SavedStateRegistryOwner
z
🙏 thanks Ian! much appreciated
Strangely, during my testing I noticed that the
ComponentDialog
never gets its
setContentView
methods called (which is what initializes the
LifecycleOwner
). The subclass
AppCompatDialog
doesn’t call
super.setContentView
and instead calls a delegate method which doesn’t end up leading to the
ComponentDialog
i
Oof, do you mind filing an issue against AppCompat to fix that? https://issuetracker.google.com/issues/new?component=460343&amp;template=1422420
At least there's an easy workaround of just calling the
setViewLifecycleOwner
with
this
(i.e., with the
ComponentDialog
instance that implements
LifecycleOwner
). That's a lot easier than building a valid owner yourself
z
👍 sure I’d be happy to, and yeah I’ve gotten that part done, now just figuring out the differences in the
SavedStateRegistryOwner
APIs in the newest savedstate artifact (1.2) vs the one appcompat activity uses (1.1.0-beta01)
118 Views