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 Jansari
  • 30,115
  • 28
  • 106
  • 129
macOsX
  • 417
  • 1
  • 8
  • 18

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
  • 20,250
  • 12
  • 87
  • 128
  • 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,425
  • 2
  • 25
  • 44