Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabHost with Activities vs. ActionBar with Fragments

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?

like image 745
Jonathan Avatar asked Nov 16 '12 15:11

Jonathan


People also ask

What is the difference between activity and fragment in Android?

Activity is the UI of an application through which user can interact and Fragment is the part of the Activity, it is a sub-Activity inside activity which has its own Life Cycle which runs parallel to the Activities Life Cycle. To read more refer to Activity Lifecycle in Android with Demo App.

How many fragments are there in the actiontab app?

If we consider the ActionTab app it has four fragments - one for each tab view. As the use taps through the tab options the fragment transitions through the states in the lifecycle diagram -- moving say from being in focus in onResume() to hidden in onPause().

How does the actiontab app work?

The ActionTab app will combine multiple fragments (four in all) in a single activity to build a multi-pane UI. These fragments can be considered self-contained components and could be reused by multiple activities potentially across multiple applications. The app is made up of 4 tabs and 4 corresponding fragments.

Can I add fragments to an activity while it is running?

We can add or remove fragments in an activity while the activity is running. It is possible to develop a UI only using Activities, but this is generally a bad idea since their code cannot later be reused within other Activities, and cannot support multiple screens.


2 Answers

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

like image 195
Marco RS Avatar answered Oct 12 '22 02:10

Marco RS


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!

like image 29
gssi Avatar answered Oct 12 '22 01:10

gssi