I am developing an application that when the button is pressed, it opens a dialog with OK and Cancel buttons.
It works fine.
When the user presses the back b
This code works:
Dialog dlg = new Dialog(thisAct, R.style.DialogTheme);
dlg.setContentView(view);
dlg.setCancelable(false);
dlg.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
dlg.setOnKeyListener((arg0, keyCode, event) -> {
Timber.d("onKey(%d)", keyCode);
//{home intercepting
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Timber.i("HOME pressed");
return true;
}
return true;
});
dlg.show();
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
}
}
Use:
MyDialog dlg = new MyDialog(context);
dlg.show();