-1

Here is an example of the code, which just doesn't work. I added this method in MainActivity

@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
    Intent intent = new Intent(this, PrefActivity.class);
    if (keyCode == KeyEvent.KEYCODE_MENU) {
        startActivity(intent);
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}
Robert
  • 35
  • 8

2 Answers2

0

If you just want to click the button without holding it down, you can use onKeyUp.

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_MENU:
            startActivity(new Intent(this, LoginActivity.class));
            return true;
    }
    return super.onKeyUp(keyCode, event);
}

If you would like to handle onKeyLongPress, then you can read more at onKeyDown and onKeyLongPress

The reason it doesn't work is your event is being consumed by onKeyDown, which is continuously fired until you let go of the key and onKeyLongPress is never called.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
-3
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {

if (keyCode == KeyEvent.KEYCODE_MENU) {
Intent intent = new Intent(getApplicationContext(), PrefActivity.class);
    startActivity(intent);
    return true;
}
return super.onKeyLongPress(keyCode, event);
}

Try it!

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
Thang BA
  • 162
  • 3
  • 13