Sounds like you want to set the OnCancelListener when you create the Dialog. It looks like this:
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
//do whatever you want the back key to do
}
});
I noticed setOnCancelListener does not work, and setOnKeyListener works, but for me has the fun side effect that it swallows all keys if your dialog has an edit text.
Override method onBackPressed() in your own dialog and use it in your code:
public class MyDialog extends Dialog {
public MyDialog(@NonNull Context context) {
super(context);
}
@Override
public void onBackPressed() {
// Do what you want
}
}