I'm trying to show a PopupWindow from a Fragment, and then add a ComposableView to the Popup contentView, and it crashes because the ViewTreeLifecycleOwner is not found for PopupDecorView$PopupDecorView.
Things I've tried:
- Use the
PopupWindowCompatto display the popup. - Multiple lib versions for
Fragment(1.3.4 and 1.4.0-alpha1). Activityversion that I'm using is: 1.3.0-alpha08.- Manually set the lifecycle owners for
contentViewandComposeView.
Calls:
Add ComposeView to contentView:
contentWindow.contentView.addView(
AdaptiveCardistRender.adaptiveCardistResponseView(
contentWindow.contentView.context,
response
)
)
Show Popup:
PopupWindowCompat.showAsDropDown(
popupWindow,
anchor,
0,
startVerticalOffset,
Gravity.NO_GRAVITY
)
Set lifecycle on the PopupWindow:
class ExtensionsPopupWindow(
val contentView: ScrollView,
width: Int,
height: Int,
lifecycleOwner: LifecycleOwner,
savedStateRegistryOwner: SavedStateRegistryOwner,
) : PopupWindow(contentView, width, height, true) {
init {
ViewTreeLifecycleOwner.set(contentView, lifecycleOwner)
ViewTreeSavedStateRegistryOwner.set(contentView, savedStateRegistryOwner)
}
Stacktrace:
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from android.widget.PopupWindow$PopupDecorView{9dfea2f V.E...... R.....I. 0,0-0,0}
at androidx.compose.ui.platform.WindowRecomposer_androidKt.createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:242)
at androidx.compose.ui.platform.WindowRecomposer_androidKt.access$createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:1)
at androidx.compose.ui.platform.WindowRecomposerFactory$Companion$LifecycleAware$1.createRecomposer(WindowRecomposer.android.kt:98)
at androidx.compose.ui.platform.WindowRecomposerPolicy.createAndInstallWindowRecomposer$ui_release(WindowRecomposer.android.kt:153)
at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:228)
at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:200)
at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.android.kt:235)
at android.view.View.dispatchAttachedToWindow(View.java:20665)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3493)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3500)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3500)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3500)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2544)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2057)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8501)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1005)
at android.view.Choreographer.doCallbacks(Choreographer.java:826)
at android.view.Choreographer.doFrame(Choreographer.java:761)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:990)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7727)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
I had the same issue when I tried to put AbstractComposeView in window popup as a workaround you can use compose Popup and still use the compose view as place holder inside view (it doesn't require height or width so it will not mess with your layout) then just show the popup with IntOffset where you want in the screen