How to use TabHost.OnTabChangeListener in android?

How to use TabHost.OnTabChangeListener in android?

give me some example code... :(

thanks


Solution 1:

why it would be my pleasure to help you good sir:

myTabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
    if(TAB_1_TAG.equals(tabId)) {
        //destroy earth
    }
    if(TAB_2_TAG.equals(tabId)) {
        //destroy mars
    }
}});

Where TAB_1_TAG is the tag provided to the newTabSpec method when creating the tab.

Solution 2:

I think in many cases it makes sense to make your TabActivity the listener:

public class MyTabActivity extends TabActivity implements OnTabChangeListener {

    private TabHost tabHost;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* Your onCreate code here */

        tabHost.setOnTabChangedListener(this);
    }

    /* ... */

    @Override
    public void onTabChanged(String tabId) {
        /* Your code to handle tab changes */
    }
}