0

I have to show a popup window on click of home screen button. I have tried using the below code onkeydown(), but on click of home key button it's not coming into the loop.

if(keyCode == KeyEvent.KEYCODE_HOME)

{

  //do something

}

Also i tried using onUserLeaveHint(), but no use.

If any one knows about this kindly let me know.

Himanshu
  • 31,810
  • 31
  • 111
  • 133
macOsX
  • 447
  • 1
  • 9
  • 19

2 Answers2

1

You can't. The API doesn't let you capture the press of that key. It's a safety key. Refer to this question for more details.

Community
  • 1
  • 1
Zoltán
  • 21,321
  • 14
  • 93
  • 134
  • thanks for your reply. is it possible to do by calling some services? – macOsX Oct 11 '12 at 08:56
  • I don't think so. Think of it as a way for Android to provide the user a mechanism to always exit the app. If it were possible, your app could render the user's phone unusable, because you could do whatever in the handling of the key event. – Zoltán Oct 11 '12 at 08:59
  • In other words: even if you could, please don't :) That's not the way Android intended to use the home button. – Zoltán Oct 11 '12 at 09:02
1

It is not possible to override the home button, it is required by the system. See this answer by the Android Developer, Romain Guy https://stackoverflow.com/a/5039586/1578771.

Also, in future its better to use onKeyUp() as you'll be able to use other events like long presses etc.

Community
  • 1
  • 1
SteveEdson
  • 2,485
  • 2
  • 28
  • 46