Mark
02/03/2023, 1:53 PMDialogFragment
) to Compose. The viewtree stuff I found by trial and error, so please let me know if there’s a better way:
fun Fragment.showDialog(dialogContents: @Composable (onDismissRequest: () -> Unit) -> Unit): AlertDialog? {
val activity = activity ?: return null
if (activity.isFinishing) {
return null
}
val view = view ?: return null
var dialog: AlertDialog? = null
dialog = MaterialAlertDialogBuilder(activity).apply {
setView(
ComposeView(activity).apply {
// doesn't seem to do make a difference, so commenting out
// setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
MdcTheme(setTextColors = true) {
dialogContents {
dialog?.cancel()
}
}
}
}
)
}.show()
val decView = dialog.window?.decorView ?: return null
decView.setViewTreeLifecycleOwner(view.findViewTreeLifecycleOwner())
decView.setViewTreeViewModelStoreOwner(view.findViewTreeViewModelStoreOwner())
decView.setViewTreeSavedStateRegistryOwner(view.findViewTreeSavedStateRegistryOwner())
return dialog
}