0

I want to handle the navigation-bar buttons. I see that I can override the onKeyDown() method in MainActivity

@Override
public boolean onKeyDown(int code, KeyEvent e) {
if(code == KeyEvent.KEYCODE_BACK){
    return true;
}else{
return super.onKeyDown(code, e);
}
}

But it is not being called.

Goku
  • 9,102
  • 8
  • 50
  • 81
Shlomo
  • 357
  • 3
  • 11

3 Answers3

1

please add into Constructor

setFocusable(true);
requestFocus();
Ara Hakobyan
  • 1,682
  • 1
  • 10
  • 21
  • 1
    They are both unresolved in the MainActivity. Where should they be added? Also, I use: dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE); to hide the navigation bar (and it is displayed only when it shows the keyboard. – Shlomo Nov 12 '17 at 06:14
0

It's getting called but you're not doing anything with it?

call finish() or something.

also you can override onBackPressed()

purush
  • 559
  • 3
  • 12
0

try to toast or log

  @Override
public boolean onKeyDown(int code, KeyEvent e) {
    if (code == KeyEvent.KEYCODE_BACK) {
        Toast.makeText(this, "on press", Toast.LENGTH_SHORT).show();
        return true;
    } else {
        return super.onKeyDown(code, e);
    }
}
xbadal
  • 1,284
  • 2
  • 11
  • 24