You have to override the Back
action and return true
to let the OS know that this action already handled and prevent executing the default action for that, which is closing the screen
Your code is valid for API level 5, but for older then API 5 use this:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}
reference: Answer
This question is asking about how to disable the Home and Recent buttons like the MXPlayer application when you click on the Lockscreen. The accepted answer says that
Since you cannot override the Home button on Android device (at least
no in the latest OS versions). MX Player draws itself on top of your
launcher when you "lock" the app and clicks on the Home button.
and gives a details description how to do that. Check it out here