I'm trying to notify my activity when the relevant tab is selected. Consider the follwoing situation:
MainActivity:
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, HomeActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("home").setIndicator("Home",
res.getDrawable(R.drawable.ic_tab_home))
.setContent(intent);
tabHost.addTab(spec);
// Do the same for the other tabs
intent = new Intent().setClass(this, ProceduresActivity.class);
spec = tabHost.newTabSpec("procedures").setIndicator("Procedures",
res.getDrawable(R.drawable.ic_tab_checklist))
.setContent(intent);
tabHost.addTab(spec);
Now, the first time my second tab is selected, the onCreate method of ProceduresActivity is called. What I want to be alble to do is, 开发者_StackOverflow社区within ProceduresActivity, to be notified whenever tab 1 and tab 2 (mine) are selected. I tryed overriding onPause and onResume within ProceduresActivity however those are only called when the MainActivity is pasued / resumed and not when I switch tabs... So in other words I want ProceduresActivity to do something whenever the user selects second tab, and not only the first time. Can someone help me please? Thanks, Luca.
Hmmm. This seems like it might be a little difficult (I'm not even sure it's possible) to achieve. There are two ways I can see that you could achieve the same results.
The first way is by have the first tab refresh it's data/information when the user goes back to it. This can be done in the onResume() and by adding a flag to it. Pretty simple.
If you need your activity to do something that will take some time, you can make an ActivityGroup. It's basically a controller for multiple activities. It starts them all at the same time and just chooses which one is shown at once. This means that (in your case) both activities would be running, but only one would be shown. When you want the first tab to do something while the user is using the second tab, you go to the ActivityGroup (which is like a controller) and tell it to do whatever you want with the first tab.
The only problem I see with the ActivityGroup route is binding it to your tabs, but there are many workarounds for this.
Maybe this can help you, i use this to perform an action when the tabs change. But maybe you can alter it to do a specific action with every tab?
getTabHost().setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String tabId) {
//TODO, action to be performed
}
}
});
精彩评论