views:

53

answers:

2

I have three tabs and each one is its own activity. When I switch tabs I want my Spinner to update but I don't know what method gets called on tab switch. Any help?

+1  A: 

Hook up a listener to the TabHost.OnTabChangeListener. You main activity extends TabActivity and its onCreate method probably looks something like this:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    TabHost tabHost = getTabHost();

    LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);

    tabHost.addTab(tabHost.newTabSpec("tab1")
            .setIndicator("tab1")
            .setContent(R.id.view1));
    tabHost.addTab(tabHost.newTabSpec("tab3")
            .setIndicator("tab2")
            .setContent(R.id.view2));
    tabHost.addTab(tabHost.newTabSpec("tab3")
            .setIndicator("tab3")
            .setContent(R.id.view3));
}

To hook up the listener, add the following code in your onCreate() method:

    tabHostt.setOnTabChangedListener(new OnTabChangeListener(){
    @Override
    public void onTabChanged(String tabId) {
        if(tabId.equals("tab1")) {
            //tab1
        }
        else if(tabId.equals("tab2")) {
            //tab2
        }
        else if(tabId.equals("tab3")) {
            //tab3
        }
    }
});

HTH

dhaag23
I really new at this, how would I go about that?I have 4 files, one that runs on app launch and every other one is an activity for a tab.
Mitchell
I've updated my answer above to include code on how to do this.
dhaag23
+1  A: 

Something like this should work:

tabHost.setOnTabChangedListener(new OnTabChangeListener()
    {
        public void onTabChanged(String tabId)
        {
            //Do stuff in here
        }
    });
kcoppock
Ok, so that now gets called when I switch my tabs! Now I just need to tell it to run a method in the activity it's switching from. Help?
Mitchell
Just add a call to the method where the "Do stuff" comment is.
kcoppock
I have another question. This executes when any tab is changed. But can I have a method inside my activity the executes when the tab that is that represents that activity is selected or de-selected.
Mitchell
Mitchell, dhaag23's answer below should help you with that.
kcoppock
I don't think I was clear and I apologize for wating your time with my lack of Android Knowledge, but I was wondering if I could have a method inside my tab's activity class that could detect when it was selected or de selected. These both reside in the TabHost file so I run into static verse non static issues.
Mitchell
Perhaps making use of the onPause and onResume methods?
Mitchell
Nothing to apologize for at all. I'm not sure exactly what you're asking for though. The onTabChanged method will fire any time a tab is selected, and you can use the if(tabId.equals("")) method dhaag23 suggested to check which tab has been selected.
kcoppock