0

Hey I have a code that looks like this:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_HOME:
            System.out.println("sdfadsfadsf");
            finish();
            return true; }
  } return super.onKeyDown(keyCode, event);}

My problem is, that neither the app terminates nor the println gets executed. Can someone tell me what I do wrong in here? The app should get closed, when a user presses the home key. I do this because of performance issues my users experience when they open up other apps. I could just make the app more performance-efficient but in this type of app, there is really no need to keep it running.

user2330482
  • 1,053
  • 2
  • 11
  • 19
  • "The app should get closed, when a user presses the home key" -- no, it should not. "I could just make the app more performance-efficient" -- since there are many ways for a user to leave your app besides pressing HOME, please "make the app more performance-efficient". – CommonsWare Apr 29 '13 at 22:30

4 Answers4

2

By design the Home key cannot be intercepted, thus KEYCODE_HOME will not be sent in any callback. You can only infer when Home key is pressed.

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • 1
    Thanks for your answer. You said, I can only infer when Home key is pressed. Doesn't that mean there would be a way to solve my problem? – user2330482 Apr 29 '13 at 21:57
  • Well yes and maybe. The yes is if your activity does not use keyboard, so no EditText or user press a hard keyboard. The maybe is you probably be able to filter out the key when keyboard is used and with logic been able to infer when the Home key is pressed. – Hoan Nguyen Apr 29 '13 at 22:04
2

You can probably try using onClose in your main. This would allow you to save files or do whatever when you close the app. Something like...

@Override
protected void onStop() {
    super.onStop();

//Do whatever
}
octupus23
  • 31
  • 5
  • Thanks a lot. Somehow, now both things work except that the app doesn't relly get closed, but thats ok because now it uses under 1 mb or RAM after pressing the home button. – user2330482 Apr 29 '13 at 22:08
  • You can probably add code to the onStop method to kill the app, I've seen a couple of posts about it... http://stackoverflow.com/questions/2092951/how-to-close-android-application. – octupus23 Apr 29 '13 at 22:11
0

I found it here in SO:

Finally, the code in your activity should be:

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

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean defaultAction = super.onKeyDown(keyCode, event);

    if (keyCode == KeyEvent.KEYCODE_HOME) {
        Log.i(TAG, "it's into the IF statement!");
    }

    return defaultAction;
}
Community
  • 1
  • 1
Nahuel Barrios
  • 1,870
  • 19
  • 22
0

This key is handled by the framework and is never delivered to applications.

but you can achieve it using Reflection. See this post

Community
  • 1
  • 1
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300