Not able to catch android back button event
Asked Answered
P

6

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.

Particle answered 16/11, 2011 at 21:3 Comment(2)
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.Newsmagazine
In the first example, when pressing the back button, does the default behavior occur?Differentiate
T
17

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

Tabber answered 16/11, 2011 at 21:9 Comment(1)
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.Tabber
M
8

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

view.setFocusableInTouchMode(True);
view.requestFocus();
Micrometer answered 16/11, 2011 at 21:24 Comment(0)
I
2

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

Ivon answered 16/11, 2011 at 21:19 Comment(2)
I'm actually trying to use the back key to dismiss own custom keyboardParticle
way late, but for anyone else who stumbles on this question, #3940627 may be very useful in case the standard Activity#onBackPressed() approach doesn't work.Ivon
T
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

Trajectory answered 16/11, 2011 at 21:29 Comment(0)
I
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.

Inwards answered 29/1, 2013 at 0:1 Comment(0)
P
-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
Provocation answered 16/11, 2011 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.