0

The question is simple, How to ignore settings or other button function when android application is running ? We have an application, everything is fine, but when we press the button (hardware on our mobile phone) the application closes with error (Unfortunately, application has stopped) We just want to ignore the settings button and close the application when the "back" (<-) button is pressed. Thank you for your answers.

Petr Župka
  • 111
  • 2
  • 9

1 Answers1

-1

In your main activity simply override onKeyDown.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_BACK)
           finish();
}
Daniel M.
  • 486
  • 5
  • 7
  • deprecated, please look at the API before posting answers – DreadHeadedDeveloper Oct 19 '14 at 13:50
  • so you're telling me onKeyDown is deprecated just because you prefer onBackPressed ? – Daniel M. Oct 19 '14 at 14:02
  • No, it's because it's acrually deprecated, anything past 5 won't take onKeyDown(), this will work for anything before that, but the new ones don't work this way – DreadHeadedDeveloper Oct 19 '14 at 14:03
  • Ok , you've got me curios , I'm looking through documentation for api level 21 and I can't seem to find where it says onKeyDown is deprecated : http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html#onKeyDown(int, android.view.KeyEvent) – Daniel M. Oct 19 '14 at 14:08
  • ???? My formal apologies. I got in trouble at work a while ago because I wrote virtually the same line of code and they told me to do it the way I told you because it was deprecated. Clearly they were wrong. I'll take back my downvote....if SO would let me – DreadHeadedDeveloper Oct 19 '14 at 14:11