views:

228

answers:

2

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?

A: 

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
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
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
A: 

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

stormin986