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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With