0

I override the home button like this :

@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_HOME: 
        System.out.println("hello");
        break;
    }
    return super.onKeyDown(keyCode, event);
}


@Override
public void onAttachedToWindow() {
    this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();
}

I create also a search interface like : http://www.edumobile.org/android/android-programming-tutorials/search-interface/

When I click on the search hardware button, the search interface comes up. But at the same time when I click on the home button, the overridden method for Home button is not called, and I go back to main screen of phone. Can you help me how can I fix it?

Ali
  • 9,800
  • 19
  • 72
  • 152

1 Answers1

2

You can not catch the Home-Button presses, see this question on SO for further details. In short: It is a system key, your application can not handle it on its own because it would allow a application to prevent the user from exiting it.

Community
  • 1
  • 1
theomega
  • 31,591
  • 21
  • 89
  • 127
  • Yeah, android cheats on several keys to maintain user experience :D – Gökhan Barış Aker May 13 '12 at 21:59
  • But I override it as you can see in the above code and it's perfectly works. Do you mean it is not possible to solve my current problem as long as I am in the search interface ( by click on search harware button) ? – Ali May 13 '12 at 22:39
  • 2
    @Ali: "But I override it as you can see in the above code and it's perfectly works" -- no, it does not. First, if it worked "perfectly", you would not have this problem. Second, this particular technique no longer works as of Android 4.0. If you want to control the HOME button, write a home screen. – CommonsWare May 13 '12 at 22:53