• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Tabhost behaviour

Furious5k

Lurker
Hi,

I have constructed a tabbed layout based on some online instructions and, while this seems to work for others, it is giving me a very odd behaviour.

If I add two tabs, it is fine. But when I add three or more tabs, the second tab and all following tabs contain the information/activity of the last added tab.

e.g. Click Tab1 - Activity1, Click Tab2 - Activity3, Click Tab3 - Activity3.

Code in next post since it thinks android layout files are links to sites.
 
Code:
[LIST=1]
[*][B][COLOR=#006699]<TabHost[/COLOR][/B] [COLOR=#ff0000]xmlns:android[/COLOR]=[COLOR=#0000ff]"http://schemas.android.com/apk/res/android"[/COLOR]
[*] [COLOR=#ff0000]android:id[/COLOR]=[COLOR=#0000ff]"@android:id/tabhost"[/COLOR]
[*] [COLOR=#ff0000]android:layout_width[/COLOR]=[COLOR=#0000ff]"fill_parent"[/COLOR]  </SPAN>
[*] [COLOR=#ff0000]android:layout_height[/COLOR]=[COLOR=#0000ff]"fill_parent"[/COLOR][B][COLOR=#006699]>[/COLOR][/B]  </SPAN>
[*] [B][COLOR=#006699]<LinearLayout[/COLOR][/B]    </SPAN>
[*]     [COLOR=#ff0000]android:id[/COLOR]=[COLOR=#0000ff]"@+id/mainLayout"[/COLOR]  </SPAN>
[*]     [COLOR=#ff0000]android:layout_width[/COLOR]=[COLOR=#0000ff]"fill_parent"[/COLOR]  </SPAN>
[*]     [COLOR=#ff0000]android:layout_height[/COLOR]=[COLOR=#0000ff]"fill_parent"[/COLOR]  </SPAN>
[*]     [COLOR=#ff0000]android:orientation[/COLOR]=[COLOR=#0000ff]"vertical"[/COLOR]  </SPAN>
[*]     [COLOR=#ff0000]android:padding[/COLOR]=[COLOR=#0000ff]"5dp"[/COLOR][B][COLOR=#006699]>[/COLOR][/B]  </SPAN>
[*]     [B][COLOR=#006699]<FrameLayout[/COLOR][/B]  </SPAN>
[*]         [COLOR=#ff0000]android:id[/COLOR]=[COLOR=#0000ff]"@android:id/tabcontent"[/COLOR]  </SPAN>
[*]         [COLOR=#ff0000]android:layout_width[/COLOR]=[COLOR=#0000ff]"fill_parent"[/COLOR]  </SPAN>
[*]         [COLOR=#ff0000]android:layout_height[/COLOR]=[COLOR=#0000ff]"wrap_content"[/COLOR]  </SPAN>
[*]         [COLOR=#ff0000]android:padding[/COLOR]=[COLOR=#0000ff]"5dp"[/COLOR]  </SPAN>
[*]         [COLOR=#ff0000]android:layout_weight[/COLOR]=[COLOR=#0000ff]"1"[/COLOR][B][COLOR=#006699]>[/COLOR][/B]              </SPAN>
[*]     [B][COLOR=#006699]</FrameLayout>[/COLOR][/B]  </SPAN>
[*]     [B][COLOR=#006699]<TabWidget[/COLOR][/B]  </SPAN>
[*]         [COLOR=#ff0000]android:id[/COLOR]=[COLOR=#0000ff]"@android:id/tabs"[/COLOR]  </SPAN>
[*]         [COLOR=#ff0000]android:layout_width[/COLOR]=[COLOR=#0000ff]"fill_parent"[/COLOR]  </SPAN>
[*]         [COLOR=#ff0000]android:layout_height[/COLOR]=[COLOR=#0000ff]"wrap_content"[/COLOR]  </SPAN>
[*]         [COLOR=#ff0000]android:layout_weight[/COLOR]=[COLOR=#0000ff]"0"[/COLOR][B][COLOR=#006699]>[/COLOR][/B]  </SPAN>
[*]     [B][COLOR=#006699]</TabWidget>[/COLOR][/B]  </SPAN>
[*] [B][COLOR=#006699]</LinearLayout>[/COLOR][/B]  </SPAN>
[*][B][COLOR=#006699]</TabHost>[/COLOR][/B]  </SPAN>
[/LIST]

Code:
[SIZE=2][LIST=1]
[*]super.onCreate(savedInstanceState);
[*]setContentView(R.layout.main);
[*]Resources res = getResources();
[*]TabHost tabHost = getTabHost();
[*]TabHost.TabSpec spec;
[*]Intent intent;
[*]intent = new Intent();
[*]intent.setClass(this, EventsActivity.class);
[*]spec = tabHost.newTabSpec("events").setIndicator(null, res.getDrawable(R.drawable.events)).setContent(intent);   </SPAN>
[*]tabHost.addTab(spec);
[*]intent.setClass(this, HistoryActivity.class);   </SPAN>
[*]spec = tabHost.newTabSpec("history").setIndicator(null, res.getDrawable(R.drawable.history)).setContent(intent);   </SPAN>
[*]tabHost.addTab(spec);
[*]intent.setClass(this, TravelActivity.class);   </SPAN>
[*]spec = tabHost.newTabSpec("travel").setIndicator(null, res.getDrawable(R.drawable.travel)).setContent(intent);   </SPAN>
[*]tabHost.addTab(spec);
[*]tabHost.setCurrentTab([COLOR=#c00000]0[/COLOR]);
[/LIST][/SIZE]
 
Looks like the culprit was the reuse of the Intent.

Code:
[SIZE=2]intent.setClass([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], Tab2.[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/B][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]

Would be interested to know why this caused no problems with 2 tabs, but started going wrong with more tabs. Can anyone explain?
 
Please don't pretty-print or line-number your code. You want to minimise the barriers to and effort required for someone to answer your question. If we can't copy and paste your code into a test project/harness, you're less likely to be answered (accurately).

(I haven't looked at tabs in Android yet, so I can't answer your question.)
 
Back
Top Bottom