Reputation: 859
I am showing a custom dialog when there is no internet connectivity. I want to do some handling when the user presses the back button while the dialog is visible.
BackHandler inside the parent screen nor within the dialog itself is working in this scenario.
Thank you
Upvotes: 5
Views: 3360
Reputation: 364451
Just define the BackHandler
function inside the Dialog
:
val shouldShowDialog = remember { mutableStateOf(true) }
if (shouldShowDialog.value) {
Dialog(onDismissRequest = { shouldShowDialog.value = false }) {
Button(onClick = {shouldShowDialog.value = false}){
Text("Close")
}
BackHandler {
// your action
}
}
}
Upvotes: 4
Reputation: 6863
Use the onDismiss
callback and disable automatic dismissal when the user taps outside the dialog. This way you can ensure that the dismiss request originated from a back press. This is a workaround, of sorts, since an out-of-the-box API is not yet bundled with Compose.
Upvotes: 2