How to use alert dialog when pressing back button?

Viewed 164

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?

1 Answers

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(//)
    }
Related