Here's the deal. I have an application with three tabs. Through various interactions with the items in the tabs I end up launching other activities. The client has reviewed this and would like the activities launched "within" the tabs, so the tabs remain visible and if the user clicks the tab it goes back to the original activity defined in the setContent function. Is this possible and how would I go about this from other activities? (ie the child activities, not the one that defines the TabHost and has access to call setContent)?
That is not possible with the existing tab framework, AFAIK. The tab framework is very simple, not designed for changes once initially set up.
commonsware.com is correct, it's not possible. I had a similar issue, but it was only 1 activity which was being launched. I sacrificed a little of my architecture and deleted the activity which was launched from inside the tab. I put the code in a View and then I added a ViewAnimator to the tab's activity. I overrode the back button and remove that view if it's up, or else let the back button perform as normal.
This faked it well enough, and for only 1 closely-related activity, I'm not going to lose any sleep over the design considerations.
It is possible to launch activities within tabs. Therefore set the tabspec content to an ActivityGroup instead of a regular Activity.
tabHost.addTab(tabHost.newTabSpec("Tab")
.setIndicator("Tab")
.setContent(new Intent(this, YourActivityGROUP.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
From within that ActivityGroup you can then start another Activity like this that only updates the contentview of the tab you're in.
class YourActivityGROUP extends ActivityGroup{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//you van get the local activitymanager to start the new activity
View view = getLocalActivityManager()
.startActivity("ReferenceName", new
Intent(this,YourActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
this.setContentView(view);
}
}
I tried hcpl's solution and it works ... almost. The problem is when I a load a ListActivity and the listview is empty. In that case it seems the focus is lost or something like that. MENU and BACK button don't do what I've set in the ListActivity. It works fine when there is at least one item in listview. Any ideas?