tags:

views:

777

answers:

4

hi all,

How to use TabHost.OnTabChangeListener in android?

give me some example code... :(

thanks

+2  A: 

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
    }
}});
sandis
A: 

Is there a way to detect if the user has pressed the already selected tab?

I can my view in the actual tab when they select an item for a list. I want to be able to reshow the list when they select the tab, but the ontabchangedlistener does not fire as the tab is already selected

Taz
A: 

What are TAB_1_TAG and TAB_2_TAG ?

A: 

I like this forum, every question has an answer :)

for question above, answer is simple: you can/should defined tab names as constants so that you can identify and use from your app easier.

here is another example:

public static final String TAB_1_TAG = "TAB_1";
public static final String TAB_2_TAG = "TAB_2";
public String CURRENT_TAB;

tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                CURRENT_TAB = tabHost.getCurrentTabTag();
                Log.d(TAG, "CURRENT_TAB:"+CURRENT_TAB);
            }
        });

for the already selected tab, I suggest to use CURRENT_TAB like variable and you set when you first time load TabActivity. e.g.

tabHost.setCurrentTabByTag(TAB_1_TAG);
CURRENT_TAB = tabHost.getCurrentTabTag();
WareNinja