Navigation with ActionBar Tabs & nested fragments - android

If you are using an Actionbar with Tabs for navigation, should we be using fragments only?
When I'm looking at the developer site, I only see examples where they are switching Fragments, not Activities. The guidelines tell us to use a ViewPager when using tabs, so you should use Fragments to make that work.
The problem is that these fragments will contain quite a lot. They should have other fragments as well. Nested fragments. These are supported when using the Support Library (or targetting api level 17).
My main concern is how the communication will run between the fragments at the bottom of the hierarchy. Will it all run through that one Activity?

You won't have problems. Just treat your fragments as something independent that will be placed in a container. That fragment can have more fragments and so on.
Some people are developing fragment based applications with a single Activity and sometimes makes sense, however I don't like that approach much.
That said, just use all the fragments you need. If you have complex data to pass between fragments you could use the activity to host it and access it in your fragments.

Related

How to handle multiple activities with fragments?

I need to implement an app through fragments.
Based on my requirements,i have a menu panel one side and other side i need to display different ui screens based on menu selection.
My Screen contains heavy ui.That why i design each screen separately.
How should i display screens based on menu selections through fragments.
If any one know the solution,Please help me
Thanks in advance.
If you mean the NavigationDrawer you find the official Tutorial on the Android Developers Website
There is also an example to download, which uses the NavigationDrawer to switch Fragments.
If you want a fixed, self-designed Menu and just want to swap out Fragments (holded by a FrameLayout) check out this Tutorial
Android officially doesn't provide more than one sliding menus (navigation drawers). You can implement SlideMenu or choose one of many such libraries for more than one sliding menus.
Once the menus are implemented, it's just a simple case of fragments and activities. There is no "direct way". Read the documentation and understand how they work. Here are some good links
Fragments - Introduction on developer site
Tutorial on multi-pane development using fragments by Mr. Vogel
Basically what you have to do is, create an Activity extending ActionBarActivity (from support library) or FragmentActivity.
create fragments and replace them in FragmentManager from your main activity.
See this for reference:
Creating a navigation drawer

Android using viewPager vs using tabhost

In my Android application I have had to use a tab layout and I want to choose a one from folowing scenarios. they are
using viewPager
using tab host
what are the advantages of viewPager over tabhost. Which one is better. And I'm targeting the android 4.2. Thank You!
It actually depends on what you want and the type of content you are putting in application.
ViewPager:
It is supported in lower API with support library and Sherlock library is always there.
Using ViewPager will give a fresh and distinctive feel to the app. For this you'll need to use fragments. Which are complicated but great in terms of performance and are replacable(a superb feature).
ViewPager
TabHost:
This is achievable using both Fragments and Activities. Although in favour of FragmentTabHost, TabActivity has been deprecated. However if you are not experienced with Fragments, use Activities, but you can always migrate to fragments later. FragmentTabHost
You should check out different tutorials and implement both of them, then decide. You should learn fragments if you embark on using ViewPager. For which, Check this
This blog has numerous examples of tabs. It can help you decide.
I found it very painful to customize tab host such as increasing indicator height, have to create 9 draw batch and customize background and many things.
With tab layout, only one line of code https://developer.android.com/reference/android/support/design/widget/TabLayout.html#setSelectedTabIndicatorHeight(int)
So, I recommend using view pager with tab layout that provides the same UX. Tab host is not deprecated, but it will happen soon, I guess :P

Actionbar tabs with multi-pane view in android

I am newbie with fragments and actionbar in android.
I want to develop application for tablets which has multiple tabs, each tab has Listview in the leftside and the details of the item on the right side
Here is an example of what i want to do.
I don't know which of this is activity and which is a fragment and how to navigate using this tabs and change the content of the listview and the details.
I want examples to navigate using tabs with multipane layout because I searched about that and I found tabs are using fragments and multipane is composed of two fragments and nested fragments not allowed in android.
And I want to know: What is the difference between activity and fragmentActivity?
Fragment Tutorials
Google "android fragment tutorial" and you'll get a lot of results like this:
http://www.vogella.com/articles/AndroidFragments/article.html
Don't stick with only one though because many of them vary just slightly and if you do two or three then you'll have a better understanding of how fragments work (standalone or multipane).
Fragments with Tabs
A great way to see an example of switching fragments is by creating a new android project in Eclipse using the latest ADT. Create a new Android Application Project and when you're setting the name of the main activity you have an option to set the navigation type. Choose tabs or tabs+swipe. This will generate a clean project for you to see the very basics of how to switch fragments using tabs.
Activity vs FragmentActivity
Essentially, an Activity may contain a layout with no fragments and a FragmentActivity may contain a layout with fragments. This answer has a few more details: Difference between Activity and FragmentActivity

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?
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
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!

Fragment Tabs and Fragment Activities

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?
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/

Categories

Resources