3

I have a TabActivity that shows other Activities as the content based on which tab is selected. What I want to do is from the 'parent' Activity try to capture the back key press, but neither onKeyDown() nor onBackPressed() ever get called in the parent... it's being handled by the 'child' Activities. Is there any way to have it pass up to the 'parent' Activity?

Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143

3 Answers3

7

Why not just using, from within the child Activity:

@Override
public void onBackPressed () {
    this.getParent().onBackPressed();
}
Andrea Baccega
  • 27,211
  • 13
  • 45
  • 46
BFil
  • 12,966
  • 3
  • 44
  • 48
2

You could set up a communication procedure between the TabActivity and his childs.

I created a static Handler in the TabActivity which was initialized in the onCreate of the TabActivity.

Then i created a static getHandler() method (in the TabActivity ).

Now a snippet to achieve your question for your childs would be:

@Override
public void onBackPressed () {
    MyTabActivity.getHandler().sendEmptyMessage(MyTabActivity.BACK_PRESSED);
}
Andrea Baccega
  • 27,211
  • 13
  • 45
  • 46
2

I have encountered the same problem as well. I sovled it this way:

@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);
}
eeerahul
  • 1,629
  • 4
  • 27
  • 38
user924829
  • 111
  • 1
  • 4