1

I follow Action bar tutorial in Android Dev site

In onCreate of ActionBarActivity.class I add tab like this;

    Tab tab = actionBar.newTab()
            .setText(R.string.artist)
            .setTabListener(new TabListener<ArtistFragment>(
                    this, "artist", ArtistFragment.class));
    actionBar.addTab(tab);

so, I will have ArtistFragment extend Fragment but I'm not sure how to add 2 fragment(ListFragment and DetailFragment) inside ArtistFragment What would be inside ArtistFragment.class?

enter image description here

April Smith
  • 1,810
  • 2
  • 28
  • 52

3 Answers3

2

What Warpzit said. Per Android: Can you nest Fragments?, Fragments cannot be nested. Your Fragment can cause another Fragment to be instantiated alongside it by reaching up to its parent Activity. I wouldn't ordinarily recommend this, as Fragments should be a unit of encapsulation. But if your Fragments are tightly coupled (for example, a detail display corresponding to a list item), I could see doing it.

Your code would look something like this:

            FragmentManager fm = getActivity().getSupportFragmentManager();
            MyFragmentClass myFragment = new MyFragmentClass();
            fm.beginTransaction().add(android.R.id.content, myFragment).commit();
Community
  • 1
  • 1
Sparky
  • 8,437
  • 1
  • 29
  • 41
1

Android doesn't support fragment in fragments (for now). You should try to make a layout that supports 2 fragments that communicate with your activity instead OR 1 fragment which handles the work that both fragments would do.

Warpzit
  • 27,966
  • 19
  • 103
  • 155
0

I've found the FragmentTabs example from the API Demos project to be very useful.

Just create a new Android sample project from Eclipse and choose the API demos (try API 13). Also for the support (compatibility) library, there's a FragmentTabs demo in the Support4Demos

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437