We can have a listener for dialog in Android:
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode,KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) {
/* The user pressed back button - do whatever here.
Normally you dismiss the dialog like dialog.dismiss(); */
}
return true;
}
});
How we can do that for AlertDialog in Jetpack Compose?
@Composable
private fun DisplayAlertDialog() {
val openDialog = remember { mutableStateOf(true) }
if (openDialog.value) {
AlertDialog(
onDismissRequest = { },
title = {
Text(
text = stringResource(id = R.string.settings),
fontSize = 18.sp
)
},
confirmButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text(stringResource(id = R.string.yes))
}
},
dismissButton = {
TextButton(
onClick = {
openDialog.value = false
}
) {
Text(stringResource(id = R.string.no))
}
},
backgroundColor = Color.White,
contentColor = Color.Black
)
}
}