https://kotlinlang.org logo
#compose
Title
# compose
p

pavi2410

06/04/2020, 6:33 PM
How do I close
AlertDialog
? I want to implement an Exit Dialog but it is neither closing when
onCloseRequest
is called nor when
confirmButton
is clicked
Copy code
@Composable
private fun ExitDialog() {
    val (show, setShow) = state { true }
    if (show) {
        AlertDialog(
            text = {
                Text("Do you want to really quit???")
            },
            confirmButton = {
                Button(modifier = Modifier.padding(16.dp), onClick = { setShow(false) }) {
                    Text(text = "OK")
                }
            },
            onCloseRequest = { setShow(false) }
        )
    } else {
        return
    }
}
h

henrikhorbovyi

06/04/2020, 6:40 PM
I know it can be the same... but try to use
value
property. It worked for me 🤔
z

Zach Klippenstein (he/him) [MOD]

06/04/2020, 6:41 PM
Are you both using dev12? I’ve had this work for me in the past too I’m pretty sure, but I haven’t tried with dev12.
p

pavi2410

06/04/2020, 6:42 PM
Yes, I'm using dev12
h

henrikhorbovyi

06/04/2020, 6:43 PM
I'm using dev12 as well, but I never faced ths problem
That's my implementation and it works.
p

pavi2410

06/04/2020, 6:52 PM
Copy code
@Composable
private fun ExitDialog() {
    val show = state { true }
    if (show.value) {
        AlertDialog(
            text = {
                Text("Do you want to really quit???")
            },
            confirmButton = {
                Button(modifier = Modifier.padding(16.dp), onClick = { show.value = false }) {
                    Text(text = "OK")
                }
            },
            onCloseRequest = { show.value = false }
        )
    }
}
This still doesn't work for me. @henrikhorbovyi I see that in your code, the initial value for the alertIsVisible state is false. I wonder how it is set to true in order to show AlertDialog?
h

henrikhorbovyi

06/04/2020, 6:53 PM
aaah
okay
I have a button that changes that
state
p

pavi2410

06/04/2020, 6:54 PM
Are state variables accessible outside the scope?
h

henrikhorbovyi

06/04/2020, 6:55 PM
Nope
They are in the same function
p

pavi2410

06/04/2020, 7:01 PM
I'm using the ExitDialog like this
Copy code
@Composable
fun Navigator(isBackPressed: MutableState<Boolean>) {
    val (currentScreen, setCurrentScreen) = state { Screen.Home }

    if (isBackPressed.value) {
        if (currentScreen == Screen.Home) {
            ExitDialog()
        } else {
            setCurrentScreen(Screen.Home)
        }
        isBackPressed.value = false
    }

    when (currentScreen) {
        Screen.Home -> Home(setCurrentScreen)
        Screen.ListScroller -> ListScroller()
        Screen.MyScreen -> MyScreen("Android Operating System")
        Screen.MarkDownParser -> MarkDownParser()
    }
}
Is there any problem with this piece of code?
z

Zach Klippenstein (he/him) [MOD]

06/05/2020, 3:07 PM
And that actually shows the dialog? It looks like it should never show it, because as soon as you add the dialog to the composition you clear the back flag and remove it from the composition.
3 Views