tags:

views:

1883

answers:

4

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)?

+1  A: 

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
This is what I was afraid of. Thank you for the response though. :)
MattC
+2  A: 

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.

marcc
+8  A: 

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);

   }
}
hcpl
thanks for this... actually I think this should be the correct way moving forward to implement this kind of functionality as it provides better code maintainability.
samiq
There is another way aswell but elegant? Not really... You can always save the instance of your TabActivity class in an extended Application class (to avoid global static). When you need to launch a new activity you call the Application class to return the TabActivity instance and launch a method which clears the tabs and recreates them but replacing the current Activity with the new one. If someone is interested I can go into more details.
gyller
A: 

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?

spiderDroid