OnKeyDown or onBackPressed in parent activity

I have a TabActivity that shows other Activities as content based on the selected tab. What I want to do is try to capture the back key from the "parent" action, but onKeyDown () nor onBackPressed () is ever called in the parent ... it is handled by the "child" actions. Is there a way to translate it into "parent" activity?

+3
source share
3 answers

Why not just use the Activity child from within:

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

TabActivity .

Handler TabActivity, onCreate TabActivity.

getHandler() ( TabActivity).

:

@Override
public void onBackPressed () {
    MyTabActivity.getHandler().sendEmptyMessage(MyTabActivity.BACK_PRESSED);
}
+2

I ran into the same problem. I tricked him like this:

@Override
public boolean dispatchKeyEvent(KeyEvent event)
{
    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
    {
        //things u want to do when back key pressed
    }    
    return super.dispatchKeyEvent(event);
}
+2
source

All Articles