Roony
Roony

Reputation: 113

How to use alert dialog when pressing back button?

I want to show alert dialog when pressing back button. But I'm facing a problem.

This is my code:

   BackHanlder{
       AlertDialogComponent()
}

And I got this error:

@composable invocations can only happen from the context of an @composable function

Any help?

Upvotes: 1

Views: 1142

Answers (1)

commandiron
commandiron

Reputation: 1473

To show alert dialog you have to keep the boolean value as state, and change it in back handler

var showAlertDialog by remember { mutableStateOf(false)}
    BackHandler {
        showAlertDialog = true
    }
    if(showAlertDialog){
        AlertDialog(//)
    }

Upvotes: 5

Related Questions