Key Events in TabActivities?
Asked Answered
H

5

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?

Hochheimer answered 8/5, 2010 at 23:33 Comment(0)
H
2

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

Hochheimer answered 27/5, 2010 at 23:57 Comment(0)
A
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.

Anthem answered 28/6, 2011 at 18:53 Comment(0)
T
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

Tanaka answered 20/12, 2010 at 16:51 Comment(0)
H
2

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

Hochheimer answered 27/5, 2010 at 23:57 Comment(0)
O
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.

Oscilloscope answered 9/5, 2010 at 0:49 Comment(3)
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.Hochheimer
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.Oscilloscope
We have found a way to handle Home event its hereSavagism
U
0

try this in your oncreate()

setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_GLOBAL);
Unbonnet answered 14/5, 2012 at 8:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.