0

The scenerio is like this. Currently I am using the following code

TabSpec setContent = tabhost.newTabSpec("tab")
                .setIndicator("tabview")
                .setContent(new Intent(tabhost.getContext(), someActivity.class));

But I am told that each tab should not be associated with an activity and we must follow code something like this.

TabSpec setContent = tabhost.newTabSpec("tab").setIndicator("tabView").setContent(R.id.layout)

Consider a scenario where tab1 calls camera app, tab2 parses an XML and tab3 does some other display work. How do I solve this ? Because as soon as tab is changed I must call these methods. How do I create a single activity and assign all responsibilities to it ?

Prabhat
  • 2,261
  • 7
  • 46
  • 74
  • Do all the initialization in the instance that holds the TabHost? is this feasible? – JQCorreia Apr 17 '11 at 16:14
  • "How do I solve this ?" -- by not putting them in tabs. Based on your description, those have nothing whatsoever to do with one another and should be separate activities (or possibly separate fragments on a Honeycomb UI), not tabs within one activity. – CommonsWare Apr 17 '11 at 17:41
  • @CommonsWave - So you are telling me assign layouts to TabSpec and call each of them as a seperate activity using Intent inside TabChangeListner ? – Prabhat Apr 17 '11 at 23:48

1 Answers1

0

You can create a single activity with tabs that show only views. The only catch is that the views have to be defined inside the tag.

<FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
      <ListView 
        android:id="@+id/list1" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="1"/>
      <ListView
          android:id="@+id/list2"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_weight="1" /> 
</FrameLayout>

Then, when inside your onCreate in your TabActivity:

TabHost tabs = getTabHost();
TabHost.TabSpec commentsTab = tabs.newTabSpec(TAB_TAG_1);
tabs.addTab(commentsTab.setContent(R.id.list1));

TabHost.TabSpec infoTab = tabs.newTabSpec(TAB_TAG_2);
tabs.addTab(infoTab.setContent(R.id.list2));

Note that I did not specify indicators for either tab, in the interests of space.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • Thanks for the reply. I understand how this works. My actual question is like this http://stackoverflow.com/questions/5784821/using-views-instead-of-an-activity-for-each-tab – Prabhat Apr 27 '11 at 06:39