Issue
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)
...
Solution
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.
Answered By - Georg
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)