Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect Hard/Soft back button on Android device?

I want to know how to detect hard or soft "BACK Button" on device? I searched but mostly I found this code

   @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

But I just need to detect if there is BACK button exist on device or not in form of hard or soft.

like image 780
user3555472 Avatar asked Jan 19 '26 17:01

user3555472


1 Answers

I think this should work

Queries the framework about whether any physical keys exist on the any keyboard attached to the device that are capable of producing the given key code.

boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);

Android developer documentation

like image 149
Ishtiaq Avatar answered Jan 22 '26 14:01

Ishtiaq



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!