4

I have created an app using Fragments throughout, as per the guidelines, so that I can show more detailed views on larger screens. However I also want to use tabs for navigation, which as per the guidelines should also be fragments rather than the deprecated TabHost.

Some Googling has shown that you can't / shouldn't nest fragments. My question is what are you meant to do if you want to have tabs, but you also want to support fragments for different size devices? Should I be removing the fragments and making two separate activities for phones and tablets?

Making it more confusing, the design guidelines here, show tabbed navigation and then what looks like a fragment activity. Is this a case of nested fragments or are they separate activities?

David Scott
  • 1,666
  • 12
  • 22

1 Answers1

3

There is an API demo a code sample that demonstrates combining a ViewPager and a TabHost to switch Fragments: Support4Demos/src/com/example/android/supportv4/app/FragmentTabsPager

If you don't like TabHost, here is another example of the same code but using ActionBar tabs and ActionBarSherlock: http://code.google.com/p/sherlock-demo/

Jeff Axelrod
  • 27,676
  • 31
  • 147
  • 246
Sparky
  • 8,437
  • 1
  • 29
  • 41
  • Thanks, I looked at them, but it didn't show how to cope with nested fragments. I ended up getting it to work by removing the nested fragments by hand when changing orientation and adding the tabs again. It's not perfect but it seems to work. – David Scott May 09 '12 at 09:47
  • You can't nest fragments. Dianne herself says so. http://stackoverflow.com/a/6847770/517561 In Android, whatever Dianne says is correct by fiat. – Sparky May 09 '12 at 10:06
  • That's what I thought. But you can see what looks like nested fragments on the new People app here: http://developer.android.com/design/patterns/multi-pane-layouts.html – David Scott May 09 '12 at 10:08
  • That doesn't look like nested fragments to me; that looks like side-by-side fragments. Don't you think so? – Sparky May 09 '12 at 10:16
  • Yes the fragments are side by side, but aren't the different tabs fragments as well? So there are 3 fragments for Groups, All and Favourites. Then on that page the All fragment looks like it has 3 side by side fragments. – David Scott May 09 '12 at 10:18
  • The tabs themselves are part of the ActionBar, which I suppose is itself a fragment. The fragments accessed by those tabs are switched in and out of the content area beneath the ActionBar. – Sparky May 09 '12 at 10:35
  • Yes, but if the All tab itself is a fragment, then the side by side view must also be fragments, which would make it nested fragments. Unless of course it's just a single fragment, with 3 listviews side by side, rather than 3 fragments side by side. – David Scott May 09 '12 at 10:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11049/discussion-between-sparky-and-david-scott) – Sparky May 09 '12 at 10:42
  • yes, im having this problem as well, if you cant have fragmentactivities as tabs how can you implement the main feature of fragments, the whole layout and fragment side by side feature???Say, you have 3 tabs as fragments, on the first tab you have a list view, if you press the list items it will go to another fragment, but if you rotate the phone that fragment should be side by side the one with the list, but for this you need a fragmentactivity to deal with the layouts or am i wrong? – Maxrunner May 23 '12 at 18:46
  • You can have, e.g., a ViewPager next to a ListFragment. See the Gmail app. – Sparky May 23 '12 at 20:10
  • Adding an update for the benifit of new visitors to this thread - Nested fragments are supported as of Android 4.2 (and Android Support Library rev 11) : . – tony m Mar 18 '13 at 06:51