java.lang.IllegalStateException:合成需要一个活跃的合成上下文(Android Jetpack Compose)

java.lang.IllegalStateException: Composition requires an active composition context (Android Jetpack Compose)

尝试使用 Jetpack Compose 显示 AlertDialog,但应用程序在调用 AlertDialog 函数时崩溃并出现错误

java.lang.IllegalStateException: Composition requires an active composition context

请在下面找到代码,

@Composable
fun homeScreenCompose() {

    Align(
        alignment = Alignment.Center
    ) {
        Column(
            arrangement = Arrangement.Center,
            modifier = Spacing(16.dp),
            children = {

                Button(
                    modifier = Height(50.dp) wraps Expanded,
                    text = "Alert Dialog", onClick = {
                        showAlertDialog()
                    }, style = OutlinedButtonStyle(
                        Border(color = Color.Red, width = 1.dp),
                        shape = RoundedCornerShape(50), //50% percent
                        contentColor = Color.Red,
                        color = Color.White,
                        elevation = Dp(4f)
                    )
                )
            }
        )
    }

}

@Composable
fun showAlertDialog() {
    val openDialog = +state { true }
    if (openDialog.value) {
        AlertDialog(
            onCloseRequest = {
            },
            title = {
                Text(text = "Logout")
            },
            text = {
                Text("Are you sure you have to logout?")
            },
            confirmButton = {
                Button("Yes", onClick = {
                    openDialog.value = false
                })
            },
            dismissButton = {
                Button("No", onClick = {
                    openDialog.value = false
                })
            },
            buttonLayout = AlertDialogButtonLayout.Stacked
        )
    }

}

无法弄清楚崩溃的原因以及解决方案, 任何帮助将不胜感激。

谢谢。

  1. 为状态模型创建一个数据class,它不必用@Model注解来标记。
  2. 初始状态本身是在 @Composable 函数中使用 + state 创建的。
  3. 对话框的可见性由调用值属性获得的模型的可见性属性决定。
  4. 这个 属性 也可以设置为一个新的不可变对象,就像在两个按钮的 onClick 中发生的那样。第一个隐藏自己,第二个 - 关闭对话。单击在同一个 @Composable 函数中定义的 Ok 按钮可以重新打开对话框。

尝试在该函数之外创建状态时,出现错误:

java.lang.IllegalStateException: Composition requires an active composition context.

上下文可以通过onCreateView中的setContent {}函数赋值获得,但是如何使用呢,比如在Presenter或者其他class以外的Fragment or Activity, 换状态还不清楚

检查下面的例子

 data class DialogVisibleModel(val visible: Boolean, val dismissPushed: Boolean = false)
    ...
 @Composable
 fun homeScreenCompose() {

   Column {
        SideBySideAlertDialogSample()
   }

 }

@Composable
fun SideBySideAlertDialogSample() {
    val openDialog = +state { DialogVisibleModel(false) }

    Button(text = "Ok", onClick = { openDialog.value = DialogVisibleModel(true) })

    if (openDialog.value.visible) {
        AlertDialog(
            onCloseRequest = {
                // Because we are not setting openDialog.value to false here,
                // the user can close this dialog only via one of the buttons we provide.
            },
            title = {
                Text(text = "Title")
            },
            text = {
                Text("This area typically contains the supportive text" +
                        " which presents the details regarding the Dialog's purpose.")
            },
            confirmButton = {
                Button("Confirm", onClick = {
                    openDialog.value = DialogVisibleModel(false)
                })
            },
            dismissButton = {
                if (!openDialog.value.dismissPushed)
                    Button("Dismiss", onClick = {
                        openDialog.value = DialogVisibleModel(true, true)
                    })
                else {
                    //hidden
                }
            },
            buttonLayout = AlertDialogButtonLayout.SideBySide
        )
    }
}