Android: How to call function of Activity inside of tab from the tabhost

trgraglia picture trgraglia · Mar 16, 2011 · Viewed 15k times · Source

I have a tabhost with three tabs. Each is an activity. I would like to have a button which is in the action bar, the bar along the top with common buttons, call functions of the tab which is active.

For example, an add function which could add something different to each tab depending on what tab was present when you clicked the button.

So, I am aksing how to call a function in Activity A from the tabHost.

And if that wont work, perhaps I can update the database from the tabhost and then refresh the tab content. Would that be easier?

Thank you all for you time and support.

Answer

Dan Kilpatrick picture Dan Kilpatrick · Jun 24, 2011

I used the following code within my TabActivity class to switch tab then call a public method defined in the activity of the tab:

getTabHost().setCurrentTab(0);
Activity MyActivity = this.getCurrentActivity();
MyActivity.myMethod();

Hopefully helpful to someone looking for the answer to this question.