Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Tabs, MapView, activities within tabs

We're in the process of writing an app that has 4 tabs: Map, People, Places, Events. The People, Places, and Events in the App show up as Icons on the map. By default the People, Places, and Events tabs each show a listview, custom rendered, displaying all the People, Places, and Events respectively.

alt text http://web6.twitpic.com/img/37202700-f92052dc474b74e1760edda1c47f6940.4adcb134-scaled.png

Now, right now each of the tabs has as its content an Intent set to launch the corresponding activity. For instance, there is a MapTabActivity that extends MapActivity, a ShowPeopleListActivity that shows the people, and so on and so forth.

I see a lot of StackOverflow questions/answers saying that due to various limitations in the way the TabHost is setup, it's best NOT to use activities as the content of tabs. For instance, it's impossible to launch a new activity and have it take the place of the existing activity within a tab, whereas it's possible to switch out a View with a different view.

Now, I'm at a crossroads. We've (for better or worse) devoted a fair amount of time trying to get this app to work the way it's currently structured, with the Activities as the content of the tabs. When an icon corresponding to a Person, Place, or Event is clicked, it fires off a VIEW Intent on a URI corresponding to that object; this is picked up by an Activity that then shows the object. The same mechanism is at work both in the Map and in the individual lists. We really like the loose coupling this provides us; we just give a VIEW command and the URI to the person/place/event and it automatically brings us to the right activity. Granted, the activity that's launched covers up the tab view rather than appearing inside of that, but we were willing to live with this.

Here's an issue though: from the Show activity, we want to be able to go back to the map, centered at that person, place, or event. We can launch a new activity to show the map again, but now we have the map activity as the content of the tab, plus the show activity, plus the new map activity in the activity stack; given how resource intensive the map activity is, I'm guessing this is not the ideal way to go.

I guess my question is, is there a GOOD tutorial somewhere showing exactly how to do complex tasks with a TabHost? I've seen HelloTabWidget; I'm looking for something much more sophisticated than this. I'm worried that if we switch to the View based way of doing things, we'll have to do a LOT of housekeeping to intercept all the back events, try to switch out the views, etc., etc., as well as strongly coupling our program in a way we don't want.

Any suggestions on a way forward would be extremely appreciated. We're new to Android so we're trying to follow the established best practices, but it's difficult when the few examples we've seen are too simplistic for our use case.

like image 990
I82Much Avatar asked Oct 19 '09 18:10

I82Much


3 Answers

I see a lot of StackOverflow questions/answers saying that due to various limitations in the way the TabHost is setup, it's best NOT to use activities as the content of tabs.

As self-appointed President of the Anti-Activity-Tab Alliance (AATA), that's certainly my position.

When an icon corresponding to a Person, Place, or Event is clicked, it fires off a VIEW Intent on a URI corresponding to that object; this is picked up by an Activity that then shows the object.

Note that this has nothing to do with having activities as the contents of tabs.

We can launch a new activity to show the map again, but now we have the map activity as the content of the tab, plus the show activity, plus the new map activity in the activity stack; given how resource intensive the map activity is, I'm guessing this is not the ideal way to go.

I'd avoid it if possible.

I'm worried that if we switch to the View based way of doing things, we'll have to do a LOT of housekeeping to intercept all the back events, try to switch out the views, etc., etc., as well as strongly coupling our program in a way we don't want.

This doesn't follow at all from what you wrote previously. Your "back events" will not change one iota between using Views as the contents of tabs and using Activities as the contents of tabs. Furthermore, this has nothing whatsoever to do with the "loose coupling" pattern you describe -- clicking on an icon in a list in a view in a tab is no different than clicking on an icon in a list in a view in an activity in a tab.

Just have your Show activity tell your, um, main activity to show a particular location, then the Show activity can finish(). The simplest way to do that without introducing a hard JVM coupling between the activities is to broadcast an Intent and register a BroadcastReceiver in the main activity. Upon receipt of this Intent, the main activity would update the map and set it to be the current tab. Of course, this approach is simpler if you have the main activity use Views for its tab contents.

Now, if you try to overhaul your application, such that navigating in a tab doesn't launch another activity, but rather keeps things within its own tab...that is a whole 'nuther kettle of fish.

like image 122
CommonsWare Avatar answered Nov 09 '22 00:11

CommonsWare


It's a good practice to have one activity and multiple views for your tabs. However, that does mean you have to be careful to handle which tab is selected, different menus and context menus for each tab view, etc.

I guess my question is, is there a GOOD tutorial somewhere showing exactly how to do complex tasks with a TabHost? I've seen HelloTabWidget; I'm looking for something much more sophisticated than this.

I wrote a slightly better tutorial on my blog that demonstrates an interacting ListView and MapView as tabs. Here's the link: Android Tabs with interacting map and list views

The basics is to have a layout similar to the one in the HelloTabWidget tutorial, make your activity extend from MapActivity, extract the tabhost from the XML and make sure you call setup() on the tabhost. After that, adding views as the content of tabs, tab listeners, etc. are the same.

Here's a brief starting point for the class:

public class TabbedListMapActivity extends MapActivity {

private ListView listView;
private MapView mapView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tabHost = (TabHost) findViewById(android.R.id.tabhost);

    // setup must be called if not a TabActivity
    tabHost.setup();

    // setup list view
    listView = (ListView) findViewById(R.id.list);

    // setup map view
    mapView = (MapView) findViewById(R.id.mapview);

    // add views to tab host
    tabHost.addTab(tabHost.newTabSpec("List").setIndicator("List").setContent(new TabContentFactory() {
        public View createTabContent(String arg0) {
            return listView;
        }
    }));
    tabHost.addTab(tabHost.newTabSpec("Map").setIndicator("Map").setContent(new TabContentFactory() {
        public View createTabContent(String arg0) {
            return mapView;
        }
    }));
}

...

like image 26
Josh Clemm Avatar answered Nov 09 '22 01:11

Josh Clemm


For instance, it's impossible to launch a new activity and have it take the place of the existing activity within a tab, whereas it's possible to switch out a View with a different view.

I don't think it is impossible. I experimented with this idea and wrote a blog article explaining what I did. I include a sample code project at the end of the article that demonstrates some of the interesting things I learned.

like image 35
ericharlow Avatar answered Nov 09 '22 01:11

ericharlow