IntelliJ Java IDE
The moose likes Android and the fly likes Intercept HOME button click Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Mobile » Android
Reply Bookmark "Intercept HOME button click" Watch "Intercept HOME button click" New topic
Author

Intercept HOME button click

yash wardhan
Ranch Hand

Joined: Jan 22, 2008
Posts: 46

Hi All,

Can we intercept HOME button click on android device or emulator.
If no, then which are the methods called before home screen is displayed on pressing HOME key?
I checked by overriding the life cycle methods of activity, it shows onPause() and onStop() are called but they are called even when we switch between the activities?
Any suggestions are appreciated

Regards,
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1365

I don't think you can intercept HOME key presses. It is reserved to ensure that you dont get locked within any application.

You can test this by coding a simple activity and overriding the onKeyDown(..) method. If you run this application and press the home button you will notice that the onKeyDown(..) callback in your code will NOT be invoked. Also, note that your onKeyDown(..) callback will be called only when(this comes straight from API docs) -
Called when a key was pressed down and not handled by any of the views inside of the activity. So, for example, key presses while the cursor is inside a TextView will not trigger the event (unless it is a navigation to another object) because TextView handles its own key presses...

So, I think that proves that you cannot intercept HOME key via onKeyDown(..) atleast. If there are any other means or if you meant something else then I dont know the answer.

As regards your second question -

Pressing the HOME button starts the launcher activity so yes, you are essentially, switching activites.
The intent fired has following properties/attributes(see DDMS->logcat for details):


CallBacks invoked when you press HOME button(listed in order) :
  • onSaveInstanceState(Bundle outState)
  • onPause()
  • onStop()

  • * Note that your activity will not be destroyed.

    Hope that helps...

    [List of FAQs] | [Android FAQ] | [My Blog] | [Samuh Varta]
    p blundell
    Greenhorn

    Joined: Aug 12, 2010
    Posts: 1
    Monu Tripathi wrote:I don't think you can intercept HOME key presses.



    Just a question, so if this Intent is printed to LogCat is there anyway catch this intent being fired by our app? and therefore releasing the home button has been pressed
    haythem souissi
    Greenhorn

    Joined: May 05, 2011
    Posts: 4

    KEYCODE_HOME : This key is handled by the frame work and is never delivered to applications.

    So you can't use it
     
     
    subject: Intercept HOME button click
     
    Threads others viewed
    Disable Keys
    Problem getting client certificate from the Web server
    Handling clicks
    Can we call two different methods based on the same action in Struts2.
    Capturing right-click from mouse in AWT
    WebSphere development made easy
    without the weight of IBM tools
    http://www.myeclipseide.com