4

I have a TabActivity and want to catch and handle presses of HOME and BACK. Where do I need to catch these events?

In my subclass of TabActivity I implement the following:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
        // Code handling
    }

    return super.onKeyDown(keyCode, event);
}

Didn't work.

So I placed a breakpoint on the switch statement line. But this function never gets called, whether I press volume up/down, menu, home, or back. Where do I need to catch these KeyEvents?

stormin986
  • 7,672
  • 15
  • 42
  • 54

5 Answers5

29

It turns out to be pretty easy. Add the following code to your child tab activity :

 @Override
  public void onBackPressed() {
    this.getParent().onBackPressed();   
  }

Then in the TabActivity do the real logic:

 @Override
  public void onBackPressed() {
    // Called by children
  }

Otherwise, the children will intercept and consume the event without notifying the tab host.

Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
Steve Zeng
  • 291
  • 3
  • 2
5

I had the same issue and found overriding dispatchKeyEvent worked.

An example of which can be found here for back button press:

http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html

Matt Gaunt
  • 9,434
  • 3
  • 36
  • 57
2

Each tab's Activity handled the "back" presses.

stormin986
  • 7,672
  • 15
  • 42
  • 54
0

try this in your oncreate()

setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_GLOBAL);
Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
patric_cena
  • 1,986
  • 1
  • 12
  • 17
0

I have a TabActivity and want to catch and handle presses of HOME and BACK. Where do I need to catch these events?

You cannot "handle presses of HOME", ever.

With respect to BACK, you can use onKeyDown() (for Android 1.x) or onBackPressed() (for Android 2.x). However, your TabActivity may be too late. For example, if you have activities as the contents of your tabs, it may be that one of them is catching the BACK press and arranging for normal processing (i.e., closing up of the activity). Since I avoid activities-as-tabs like the plague (except for one book example), I have not experimented with BACK button processing in that scenario.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm developing for 1.5 so I'm overriding `onKeyDown()`. I *am* using activities for each tab, and even tried to override in one of my particular tab activities. It still never triggered the `onKeyDown()` function there, however. – stormin986 May 09 '10 at 01:02
  • I have no clue, then. Consider switching to having views, rather than activities, as the contents of your tabs. Not only will that save a bunch of system resources, but it should simplify the key event flow, such that your TabActivity would get `onKeyDown()` calls. – CommonsWare May 09 '10 at 11:16
  • We have found a way to handle Home event its [here](http://stackoverflow.com/questions/10025660/override-home-and-back-button-is-case-a-boolean-is-true/10025904#10025904) – Lalit Poptani Apr 11 '12 at 06:49