0

I have the following code in my Activity. I have Breakpoints set to all of these methods. When I press the Menu button, it enters onKeyDown and onKeyUp. When I press the back button, the app closes and phone goes back to home screen without passing any of my breakpoints.

EDIT: This could should work according to most sources:

@Override
public void onBackPressed() {
    SwitchToMenuState();
}

However, it doesn't. I have tried all permutations of the following code blocks, and neither of them ever get called:

@Override
public void finish()
{
    SwitchToMenuState();
}   

public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        SwitchToMenuState();
    }
    return true;
}

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        SwitchToMenuState();
    }
    return true;
}   

I am using Android 2.3 and Processing, although it doesn't seem to be related to Processing at all. Eclipse tells me that I am overriding android.app.Activity.onBackPressed.

joon
  • 832
  • 13
  • 31
  • What Android version are you running this on? – Andrei Dec 01 '12 at 16:59
  • Most likely onBackPressed() is indeed being called, but there's something up with your SwitchToMenuState() method. Maybe you have some incorrect condition checking in there or something. Mind posting that up? – Jade Byfield Dec 01 '12 at 18:48
  • I have a breakpoint on the method, so it is not being called (since the breakpoints on other methods do work, when other buttons are pressed). I have tried putting Log.d("myTag","OnBackPressed Registered"); in the onBackPressed method, without luck. – joon Dec 01 '12 at 19:52

3 Answers3

1

All you need is onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {

        //Your code here

            return true;
    }
    return super.onKeyDown(keyCode, event);
}

I think it's because you're not returning the super part. This works for me, you don't need the onbackpressed or onkeyup etc.

AndroidPenguin
  • 3,445
  • 2
  • 21
  • 42
0

Use onKeyDown in android 1.x onBackPressed in android 2.x

And use only either of them, not both

cjds
  • 8,268
  • 10
  • 49
  • 84
  • I have tried either individually. I am on Android 2.3. The code never enters the onBackPressed method. Eclipse confirms that I am overriding onBackPressed from android.app.Activity.onBackPressed. – joon Dec 01 '12 at 18:39
  • unless you're adding super.onBackPressed() to the top of the method it should run. Be sure the onKeyDown method stub isn't there because even that will prevent a call to onBackPressed unless super.onKeyDown(....,...) is called – cjds Dec 02 '12 at 03:03
  • Maybe this will help http://android-developers.blogspot.in/2009/12/back-and-other-hard-keys-three-stories.html – cjds Dec 02 '12 at 03:04
-1

when you press back, if haven't a activity before this activity, your app will close. You can start use : startActivity(intent);

  • I'm not sure I fully understand what you mean. My 'main' class extends Activity, and this is where that code is (which works for all other buttons except back). – joon Dec 01 '12 at 16:53