TabHost with Activities vs. ActionBar with Fragments
Asked Answered
K

2

9

I've found some discussions related to this issue but nothing clear or definitive.

Building a tab-based application, one seems forced to choose between (A) using TabHost with Activities for each screen, and (B) using the ActionBar with Fragments for each screen. This raises a few questions:

1) Is that dilemma for real, or can one use the ActionBar with different Activities.

2) If the dilemma is for real, why are things set up this way? Is Google planning to deprecate TabHost and the multiple-Activities approach to tab-based navigation? Is there something unsavory about the multiple-Activities approach?

3) If both approaches will continue to be supported, what are the pros and cons of each? If I go with ActionBar+Fragments, will I run into any gotchas down the line? For example, when I want one of my tabbed screens to slide over/pop-on an additional screen/Fragment, will I get funny behavior switching to/from tabs in the ActionBar?

Kristinkristina answered 16/11, 2012 at 15:56 Comment(0)
M
3

You also have the third option of using a TabHost with Fragments. This makes it to where you only have one activity and multiple fragments as the children.

http://developer.android.com/reference/android/support/v4/app/FragmentTabHost.html

I have created a simple example using this class.

https://github.com/marsucsb/nested-fragments

Moll answered 16/11, 2012 at 19:47 Comment(7)
Right, that is a third option. Does it have any advantages over ActionBar+Fragments though? The docs recommend the ActionBar for tabbed navigation for consistency and other reasons, and it sounds like TabHost may get deprecated. As long as I'm doing the whole application in fragments, is there any reason to go TabHost?Kristinkristina
I think it is just a matter of preference and need specific for your application. I personally do not usually use TabHost over the ActionBar+Fragments+ViewPager approach because I like the effect of being able to swipe through fragment pages.Moll
Cool, thanks! Btw, a different solution I'm fiddling with right now: use ActionBar tabs, but with a separate Activity for each tab by building an identical ActionBar for each Activity. With the animations suppressed when switching between activities, it's working surprisingly well. Feels hacky, but might end up being more elegant than all-Fragments when it comes to navigating forward/back under a single tab (ViewPager may not serve my purposes, need something more like iPhone's UINavigationController). See any impending doom for this approach?Kristinkristina
Wow, that set up is a little hard to imagine. I think you should be okay but I always worry about maintainability. Have you looked into possibly using fragments with a ViewFlipper to flip between views and a stack that maintains the viewflipper state. Ive been slowly trying to get alway from using all activities. It seems like fragments are the way to go due to how easy it is to reuse them and the role they play when supporting tablets.Moll
ViewFlipper is a good idea, but the navigation forks--I'll need to flip to different fragments depending on what the user selects.Kristinkristina
@Kristinkristina One item to keep in mind is that the ActionBar is not supported pre API 11. If you want to target 40%+ of the Android user base you will have to address not having an ActionBar in that user base.Backtrack
Hey Marco. Is it possible to modify your example that I can have other elements beside the ChildTabHost in the TabHostParentFragment? I already created a question for this, here (It doesn't have to be an ActionBar)Hillari
F
1

I'm with you. I began developing "Droid Bones" (be sure to use the quotes) before the advent of Fragments. When I began looking at that architecture, it appeared that it would not accommodate my structural requirements. That said, I've found the multiple-activity approach to be very fluid and flexible. My concern is that some of the TabHost documentation appears to have 'gone missing' of late!

Feverous answered 16/11, 2012 at 16:6 Comment(5)
Can you elaborate on the structural limitations of the Fragments approach? What did "Droid Bones" require that couldn't be done in Fragments? (Also, docs disappearing is concerning! What's gone missing?)Kristinkristina
The main concern I had was that I employ a couple of spinners above the TabWidget and I couldn't find a way of doing this with the ActionBar construct. Also, I was concerned that I also employ a TabWidget in the child Activity frames which I couldn't see the ActionBar accommodating.Feverous
As for the missing docs, there used to be a sample project named "HelloTabWidget". I don't see this anywhere in the SDK samples directory.Feverous
Apparently the major problem with ActionBar tabs is the back button logic, which doesn't behave well. Seems you have to create a custom back stack and handle the back-button logic yourself, in an override of onBackPressed(). Weak sauce, Google.Kristinkristina
There's some discussion of the back stack issue here and here.Kristinkristina

© 2022 - 2024 — McMap. All rights reserved.