Tuesday, 22 October 2013

tab host back stack (back button)

Don't handle KeyEvent.KEYCODE_BACK in your tab activitys, do it in your main TabActivity.
As long as your tab activitys onKeyDown() methods don't handle it but they do callsuper.onKeyDown(keyCode, event); it will filter up to your TabActivity.
In your TabActivity have a member int currentTab = 0 then in the TabActivity do this...




@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (currentTab == 0)
            currentTab = 2;
        else
            currentTab--;
        tabHost.setCurrentTab(currentTab);
        return true;
    }
    else
        return super.onKeyDown(keyCode, event); 
}

No comments:

Post a Comment