amar_1995
12/31/2019, 10:03 AM@Composable() () -> Unit and () -> Unit function signature.
I am using AlertDialog and in documenation (https://developer.android.com/reference/kotlin/androidx/ui/material/package-summary#alertdialog) the function signature is different then in code.
In documentation it is:
@Composable fun AlertDialog(
onCloseRequest: () -> Unit,
title: () -> Unit = null,
text: () -> Unit,
confirmButton: () -> Unit,
dismissButton: () -> Unit = null,
buttonLayout: AlertDialogButtonLayout = AlertDialogButtonLayout.SideBySide): Unit
In dev3 code it is:
@Composable
fun AlertDialog(
onCloseRequest: () -> Unit,
title: @Composable() (() -> Unit)? = null,
text: @Composable() () -> Unit,
confirmButton: @Composable() () -> Unit,
dismissButton: @Composable() (() -> Unit)? = null,
buttonLayout: AlertDialogButtonLayout = AlertDialogButtonLayout.SideBySide
) : Unitamar_1995
12/31/2019, 10:09 AMAlertDialog inside navigation drawer and getting exception java.lang.IllegalStateException: Composition requires an active composition context .
My code:
Clickable(onClick = {
val isOpen = +state { true }
if(isOpen.value) {
AlertDialog(onCloseRequest = {isOpen.value = !isOpen.value}, text = {Text("Hello World!!!")}) {
Button(
text = "CLOSE",
style = TextButtonStyle(),
onClick = { isOpen.value = !isOpen.value }
)
}
}
}) {.... }
This code is written inside the ModelDrawerLayout drawerContent functioncurioustechizen
12/31/2019, 10:41 AM@Composable to a function effectively changes the signature of the function. @Composable fun Foo() is not compatible with fun Foo() and it is a compile error to pass one where another is expected.
It is similar to how adding suspend modifier changes the signature of a functionamar_1995
12/31/2019, 10:59 AM@Composable function cannot be call inside of normal function else it will throw Composition requires an active composition context exception.
But, I don't know why i am getting that error, when I am using AlertDialog inside ModelDrawerLayout.
And why there is difference in function signature in code and docs.msink
12/31/2019, 11:12 AMdev3 releaseMarc Knaup
12/31/2019, 12:23 PMdismissButton: () -> Unit = null
That isn’t even valid as the type isn’t nullable.
And the Android documentation should indeed use @Composable here. The dev3 code that you’ve pasted is correct.
@Composable () -> Unit makes it clear that another child component is expected (composed) - i.e. it’s not a callback/handler.amar_1995
12/31/2019, 12:28 PMAlertDialog inside ModelDrawerLayout
Clickable(onClick = {
val isOpen = +state { true }
// todo some alert box with field
if(isOpen.value) {
AlertDialog(onCloseRequest = {isOpen.value = !isOpen.value}, text = {Text("Hello World!!!")}) {
Button(
text = "CLOSE",
style = TextButtonStyle(),
onClick = { isOpen.value = !isOpen.value }
)
}
}
}) {
Container(width = 20.dp, height = 20.dp) {
DrawImage(image = +imageResource(R.drawable.image_add))
}
}Marc Knaup
12/31/2019, 12:32 PMAlertDialog inside an onClick listener which is not allowed. You must create the AlertDialog in the same context where you create the Clickable and all other components, likely in setContent {} of your Activity or something similar.Marc Knaup
12/31/2019, 12:33 PMMarc Knaup
12/31/2019, 12:34 PMamar_1995
12/31/2019, 12:36 PMcomposable ?Marc Knaup
12/31/2019, 12:38 PMonClick is called much later when the UI was already composed (when the user clicked the button).
So you change the state using isOpen.value = true which tells Android to compose the whole UI again and the cycle repeats (compose whole UI again - which happens after onClick is done already).amar_1995
12/31/2019, 12:52 PM