pavi2410

    pavi2410

    2 years ago
    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
    @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
        }
    }
    henrikhorbovyi

    henrikhorbovyi

    2 years ago
    I know it can be the same... but try to use
    value
    property. It worked for me 🤔
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    2 years ago
    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.
    pavi2410

    pavi2410

    2 years ago
    Yes, I'm using dev12
    henrikhorbovyi

    henrikhorbovyi

    2 years ago
    I'm using dev12 as well, but I never faced ths problem
    That's my implementation and it works.
    pavi2410

    pavi2410

    2 years ago
    @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?
    henrikhorbovyi

    henrikhorbovyi

    2 years ago
    aaah
    okay
    I have a button that changes that
    state
    pavi2410

    pavi2410

    2 years ago
    Are state variables accessible outside the scope?
    henrikhorbovyi

    henrikhorbovyi

    2 years ago
    Nope
    They are in the same function
    pavi2410

    pavi2410

    2 years ago
    I'm using the ExitDialog like this
    @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?
    Zach Klippenstein (he/him) [MOD]

    Zach Klippenstein (he/him) [MOD]

    2 years ago
    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.