Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key Events in TabActivities?

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?

like image 515
stormin986 Avatar asked Nov 28 '22 01:11

stormin986


1 Answers

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.

like image 111
Steve Zeng Avatar answered Dec 24 '22 11:12

Steve Zeng