How to Handle Back Button with in the Dialog

How to handle Back button with in the dialog?

dialog.setOnKeyListener(new Dialog.OnKeyListener() {

@Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
finish();
dialog.dismiss();
}
return true;
}
});

Handle Back button in AlertDialog.Builder

If you want to finish the current Activity when Negative Button of AlertDialog is clicked or hardware back button is clicked(when AlertDialog is shown) then do something like below,

pDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// if from activity
finish();

}

});

Detect Back Button Event when Dialog is Open

this worked for me...

       yuordialog.setOnKeyListener(new Dialog.OnKeyListener() {

@Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//your stuff....
}
return true;
}
});

Detect back button press while dialog is open in flutter

Back button won't close the dialog.

showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return WillPopScope(
onWillPop: () async => false,
child: AlertDialog(
title: Text('Title'),
content: Text('This is Demo'),
actions: <Widget>[
FlatButton(
onPressed: () => Navigator.pop(context),
child: Text('Go Back'),
),
],
),
);
},
);

How to override the back button on a showDialog in Flutter?

I'm not sure if understand what do you want exactly, but if you want to cancel the back button effect while the dialog box in the screen you can wrap it in WillPopScope too like this :

class HomePage  extends StatelessWidget {

@override
Widget build(BuildContext context) {

Future<bool> exitConfirm() async {

return await showDialog(
barrierDismissible : false,
context: context,
builder: (context) => WillPopScope(
onWillPop: () async => false,
child: AlertDialog(
content: Row(
children: [
CircularProgressIndicator(),
Container(margin: EdgeInsets.only(left: 7),child:Text("Logging out..." )),
],),

),
),
)??false;
}

return WillPopScope(
onWillPop:exitConfirm,
child: Scaffold (

appBar: AppBar(),
body: const Center(
child: Text('Test page'),
)
),
);

}

}

The result:

Sample Image

notice that I have added this line to the showDialog so when you click on the screen the dialog doesn't dismiss :

barrierDismissible : false,

showing alert dialog on pressing back button

Please check the following code snippet

 @Override
public void onBackPressed() {
//super.onBackPressed();
IsFinish("Want to close app?");
}

public void IsFinish(String alertmessage) {

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

switch (which) {
case DialogInterface.BUTTON_POSITIVE:
android.os.Process.killProcess(android.os.Process.myPid());
// This above line close correctly
//finish();
break;

case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(alertmessage)
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();

}

How to handle hardware Back Button on Ons-Dialog?

I had the same problem when using the dialog component from within an ons-navigator object. In order to make it work, I had to disable the default back button handler of the dialog and have the Navigator object handle it instead.

Here is the code I made, hope it helps:

ons.createDialog('dialogs/yourDialog.html').then(function(dialog) {
dialog.getDeviceBackButtonHandler().disable();

var f = function(event) {
dialog.hide();
myNavigator.getDeviceBackButtonHandler().setListener(function(event) {
try{
myNavigator.popPage();
}
catch (err){
event.callParentHandler();
}
});
}
myNavigator.getDeviceBackButtonHandler().setListener(f);
dialog.show({parentScope: $scope});
});

Brief explanation:

  • Disable dialog back button handler (this is what is causing errors)
  • When disabled, the back button will be handled by the next node that has a back button handler (myNavigator, in this case) that works fine.
  • Change the event listener of myNavigator when the dialog is shown, to hide the dialog.
  • After hiding it, I try to restore its default functionality. It´s a
    Navigator object, so popPage should be the default action, and, if
    the page stack is empty (what throws an error) we will call the
    parent handler (that will, usually, get you out of the app)

DialogFragment and back button

It's hard to say for sure what the issue is, since you haven't posted any code. But my first guess is that you haven't added the DialogFragment to the back stack by calling the addToBackStack method of the FragmentTransaction that you're using to add your fragment to the activity.

There are examples right in the Android documentation pages that give examples of a good pattern for using a DialogFragment in your Activity.

Since you are displaying a Dialog, the created Dialog will receive the key events, not the parent Activity. So, set a Dialog.OnKeyListener when you create the Dialog's fragment, and call setCancelable(false) on the Dialog to prevent the back key from dismissing it. You can then handle the back key in your OnKeyListener's onkey method.



Related Topics



Leave a reply



Submit