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.
Asked
Active
Viewed 89 times
0
-
No, it's not simple, you have not explained anything/ – DreadHeadedDeveloper Oct 19 '14 at 13:36
-
i have just edited the question, hope you get it now. – Petr Župka Oct 19 '14 at 13:40
-
much better, thank you, – DreadHeadedDeveloper Oct 19 '14 at 13:41
-
could you please describe it more complex ? – Petr Župka Oct 19 '14 at 13:42
-
have you looked at the android api? They listen to back button presses and you can easily handle this sort of issue – DreadHeadedDeveloper Oct 19 '14 at 13:43
-
could you please provide more info, or some step-by step guide ? – Petr Župka Oct 19 '14 at 13:46
-
well here's a good place to start. http://stackoverflow.com/questions/5312334/how-to-handle-back-button-in-activity – DreadHeadedDeveloper Oct 19 '14 at 13:50
1 Answers
-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