I'm trying to catch the back key event using boolean onKeyDown(int keyCode, KeyEvent event)
while showing the soft keyboard. But it is not working...
Asked
Active
Viewed 6,830 times
0
-
Here the way, I catched the back key event. http://stackoverflow.com/a/40340213/3957916 – Sumit Saurabh Oct 31 '16 at 10:23
2 Answers
1
override this method of Edittext:
@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
//put your logic here
}
return super.onKeyPreIme(keyCode, event);
}
you will get event on backpress inside if.

Chirag Shah
- 2,058
- 2
- 20
- 30
-1
Try the following:
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
//your code
return true;
}
Warning: You are informing Android system that you already handled the back key press and it will not perform as intended inside the Application (will not exit).

Mohamed_AbdAllah
- 5,311
- 3
- 28
- 47
-
-
From the Document, I think it is possible :Key presses in software keyboards will generally NOT trigger this listener, *although some may elect to do so in some situations* – Mohamed_AbdAllah Jul 30 '12 at 10:40