5

I am trying to catch the back button event for Android. I know there is a lot about this already on the forms, however, my code does not work as the examples given. Here is my code snippet to capture the event:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
    if(keyCode == KeyEvent.KEYCODE_BACK){
        Log.d(TAG, "back key captured");
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

I also tried this:

@Override
public void onBackPressed(){
    Log.d(TAG, "in onBackPressed");
    finish();
}

The output from LogCat that either event got fired doesn't show up. Anyone know a possible reason for this?

Thanks.

coder
  • 10,460
  • 17
  • 72
  • 125
  • 1
    Start by adding a log before your test to make sure you go in the method. If not, make sure your View is the one currently focused. If not it will not get the onKey events. – ol_v_er Nov 16 '11 at 21:15
  • In the first example, when pressing the back button, does the default behavior occur? – Thomas Williams Nov 16 '11 at 21:22

6 Answers6

17

Another method to is to override the public void onBackPressed() method. It's more straightforward and easier to do.

NikiC
  • 100,734
  • 37
  • 191
  • 225
Brian
  • 7,955
  • 16
  • 66
  • 107
  • 3
    Really? That's quite strange, you are doing this from an ordinary Activity and nothing is showing up at all? Perhaps try making it show a toast when the back button is pressed just to double check that your debugger isn't having problems. – Brian Nov 16 '11 at 21:46
8

To receive a keyboard event, a View must have focus. To force this use:

view.setFocusableInTouchMode(True);
view.requestFocus();
Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
2

Is the soft keyboard showing? That view will capture the back key first to dismiss it before your own code can handle it.

qix
  • 7,228
  • 1
  • 55
  • 65
  • I'm actually trying to use the back key to dismiss own custom keyboard – coder Nov 16 '11 at 21:20
  • 1
    way late, but for anyone else who stumbles on this question, http://stackoverflow.com/questions/3940127/intercept-back-button-from-soft-keyboard may be very useful in case the standard Activity#onBackPressed() approach doesn't work. – qix Feb 14 '12 at 00:52
1

My situation may be unusual, but I had the exact same behaviour so I thought I'd share with the class! The reason ended up being that inside my onResume() event for the Activity, I was starting another activity. That activity was ending itself before it ever displayed any UI, but it meant that the "onResume" for my main activity was continually being called whenever the sub-activity finished, and the back button events all seemed to disappear into the sub-activity.

Chris Rae
  • 5,627
  • 2
  • 36
  • 51
1

This works for me

private long lastBackPressTime = 0;
    @Override
public void onBackPressed() {
    if (this.lastBackPressTime < System.currentTimeMillis() - 4000) {
        Toast.makeText(this, R.string.backButtonWarning, 4000).show();
        this.lastBackPressTime = System.currentTimeMillis();
    } else {
        super.onBackPressed();
    }
}

Lately iv'e been experimenting a HUGE amount of problems due to the ADT plugin, that doest generate te apropiate resource file (R). So double check you have Cleaned your project

dinigo
  • 6,872
  • 4
  • 37
  • 48
-1

Here is how I implemented handling the back-key pressed event.

/**
   * onKeyDown method
   * 
   * Executes code depending on what keyCode is pressed.
   * 
   * @param int keyCode
   * @param KeyEvent
   *          event KeyEvent object
   * 
   * @return true if the code completes execution, false otherwise
   * 
   */
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {        
    switch (keyCode) {
    case KeyEvent.KEYCODE_BACK:
      Log.d(TAG, "back key captured");

      this.onBackPressed();

      //You could also use this.moveTaskToBack(true) to return to the Home screen

      return true;

    default:
      return super.onKeyDown(keyCode, event);
    }
  }// end onKeyDown
Bryan
  • 3,629
  • 2
  • 28
  • 27