Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BottomSheetDialog containing ComposeView

I am trying to fill a bottomsheet dialog with a compose view inside an AppCompatActivity:

val bottomSheet = BottomSheetDialog(this)

bottomSheet.setContentView(ComposeView(this).apply {
    setContent {
        MaterialTheme {
            Test()
        }
    }
})

bottomSheet.show()

using androidx.fragment:fragment-ktx:1.4.0-alpha10, androidx.appcompat:appcompat:1.3.1

getting the following exception:

    java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from android.widget.FrameLayout{386906d V.E...... ......I. 0,0-0,0 #7f0900d3 app:id/container}
        at androidx.compose.ui.platform.WindowRecomposer_androidKt.createLifecycleAwareViewTreeRecomposer(WindowRecomposer.android.kt:244)
        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:99)
        at androidx.compose.ui.platform.WindowRecomposerPolicy.createAndInstallWindowRecomposer$ui_release(WindowRecomposer.android.kt:155)
        at androidx.compose.ui.platform.WindowRecomposer_androidKt.getWindowRecomposer(WindowRecomposer.android.kt:230)
        at androidx.compose.ui.platform.AbstractComposeView.resolveParentCompositionContext(ComposeView.android.kt:220)
        at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.android.kt:227)
        at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.android.kt:259)
        at android.view.View.dispatchAttachedToWindow(View.java:19553)
...

like image 384
Georg Avatar asked Jul 20 '26 09:07

Georg


1 Answers

I ended up subclassing BottomSheetDialogFragment and using a compose view there:

class MyBottomSheet : BottomSheetDialogFragment() {
    
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View = ComposeView(requireContext()).apply {
            setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
            setContent {
                // working
            }
    }
}

And calling it from the activity like this:

val modalBottomSheet = MyBottomSheet()
modalBottomSheet.show(supportFragmentManager, "SomeTAG")

Just in case anyone runs into the same problem.

like image 161
Georg Avatar answered Jul 22 '26 21:07

Georg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!