4

I'd like to show a different fragment in a TabHost, for every tab. But it seems that there's no easy way to do this. TabHost can only accept A. Views or B. Intents that launch Activities when the user selects a tab.

Going with A means that I have to initialize every fragment and load them into container Views that are given to the TabHost. But I want these fragments to load only when needed - when the user selects their tab that is.

Going with B means that I load the fragments into separate Activities for each tab. But I'd like the fragment to be able to reach the "original" parent Activity, not just some shell Activity that hosts them in a tab content.

Am I missing something? Is there a way to manage fragments with TabHost properly?

Zsombor Erdődy-Nagy
  • 16,864
  • 16
  • 76
  • 101

1 Answers1

9

Some hits here:

  1. fragment Support with Tabhost
  2. Android 1.6 & Fragment & Tabhost

For what I understood, "link" a fragment inside a TabHost isn't allowed, you need to create activities that include your fragments and then call these activities from TabHost.

EDIT the "official solution" from Google, and with nice animations.

Community
  • 1
  • 1
Rainbowbreeze
  • 1,503
  • 1
  • 9
  • 14
  • the "official solution" should remain. please delete the other links – fmo Apr 23 '12 at 22:45
  • 1
    The official solution redirects to samples page, which says to download the samples. In the ApiDemos FragmentTabs only uses ActionBar tabs, ordinary tabs are done using deprecated TabActivity. I would appreciate if someone clarified which is the preferred way, use deprecated or not? – comodoro Sep 25 '12 at 09:39
  • This link is quite useful: http://thepseudocoder.wordpress.com/2011/10/04/android-tabs-the-fragment-way/ – comodoro Sep 25 '12 at 09:52