In Android API 11+, Google has released a new class called
What’s the purpose of fragments and some possible uses of them (other than some UI examples that can be easily be achieved by simple views/layouts)?
My question is about fragments:
- What are the purposes of using a fragment?
- What are the advantages and disadvantages of using fragments compared to using activities/views/layouts?
- Can you give some really interesting uses for fragments? Things that Google didn’t mention in their videos?
- What’s the best way to communicate between fragments and the activities that contain them?
- What are the most important things to remember when you use fragments? Any tips and warnings from your experience?
#1 & #2 what are the purposes of using a fragment & what are the
advantages and disadvantages of using fragments compared to using
Fragments are Android’s solution to creating reusable user interfaces. You can achieve some of the same things using activities and layouts (for example by using includes). However; fragments are wired in to the Android API, from HoneyComb, and up. Let me elaborate;
ActionBar. If you want tabs up there to navigate your app, you quickly see that
ActionBar.TabListenerinterface gives you a
FragmentTransactionas an input argument to the
onTabSelectedmethod. You could probably ignore this, and do something else and clever, but you’d be working against the API, not with it.
FragmentManagerhandles «back» for you in a very clever way. Back does not mean back to the last activity, like for regular activities. It means back to the previous fragment state.
You can use the cool
FragmentPagerAdapterto create swipe interfaces. The
FragmentPagerAdaptercode is much cleaner than a regular adapter, and it controls instantiations of the individual fragments.
Your life will be a lot easier if you use Fragments when you try to create applications for both phones and tablets. Since the fragments are so tied in with the Honeycomb+ APIs, you will want to use them on phones as well to reuse code. That’s where the compatibility library comes in handy.
You even could and should use fragments for apps meant for phones only. If you have portability in mind. I use
ActionBarSherlockand the compatibility libraries to create “ICS looking” apps, that look the same all the way back to version 1.6. You get the latest features like the
ActionBar, with tabs, overflow, split action bar, viewpager etc.
The best way to communicate between fragments are intents. When you press something in a Fragment you would typically call
StartActivity() with data on it. The intent is passed on to all fragments of the activity you launch.
Not sure what video(s) you are referring to, but I doubt they are saying you should use fragments instead of activities, because they are not directly interchangeable. There is actually a fairly detailed entry in the Dev Guide, consider reading it for details.
In short, fragments live inside activities, and each activity can host many fragments. Like activities, they have a specific lifecycle, unlike activities, they are not top-level application components. Advantages of fragments include code reuse and modularity (e.g., using the same list view in many activities), including the ability to build multi-pane interfaces (mostly useful on tablets). The main disadvantage is (some) added complexity. You can generally achieve the same thing with (custom) views in a non-standard and less robust way.
A Fragment is a piece of an application’s user interface or behavior that can be placed in an Activity which enable a more modular activity design. It will not be wrong if we say a fragment is a kind of subactivity.
Following are important points about a fragment:
A fragment has its own layout and its own behavior with its own lifecycle callbacks.
You can add or remove fragments in an activity while the activity is running.
You can combine multiple fragments in a single activity to build a multi-pane UI.
A fragment can be used in multiple activities.
The fragment life cycle is closely related to the lifecycle of its host activity.
When the activity is paused, all the fragments available in the acivity will also be stopped.
A fragment can implement a behavior that has no user interface component.
Fragments were added to the Android API in Android 3 (Honeycomb) with API version 11.
For more details, please visit the official site, Fragments.
This is important information that I found on fragments:
Historically each screen in an Android app was implemented as a separate Activity. This creates a challenge in passing information between screens because the Android Intent mechanism does not allow passing a reference type (i.e. object) directly between Activities. Instead the object must be serialized or a globally accessible reference made available.
By making each screen a separate Fragment, this data passing headache
is completely avoided. Fragments always exist within the context of a
given Activity and can always access that Activity. By storing the
information of interest within the Activity, the Fragment for each
screen can simply access the object reference through the Activity.
Fragments are of particular use in some cases like where we want to keep a navigation drawer in all our pages. You can inflate a frame layout with whatever fragment you want and still have access to the navigation drawer.
If you had used an activity, you would have had to keep the drawer in all activities which makes for redundant code. This is one interesting use of a fragment.
I’m new to Android and still think a fragment is helpful this way.
Activities are the full screen components in the app with the toolbar, everything else are preferably Fragments.
One full screen parent activity with a toolbar can have multiple panes, scrollable pages, dialogs, etc. (all fragments), all of which can be accessed from the parent and communicate via the parent.
Activity A, Activity B, Activity C:
- All activities need to have same code repeated, to show a basic
toolbar for example, or inherit from a parent activity (becomes
cumbersome to manage).
- To move from one activity to the other, either all of them need to be in memory (overhead) or one needs to be destroyed for the other to open.
- Communication between activities can be done via Intents.
Activity A, Fragment 1, Fragment 2, Fragment 3:
- No code repetition, all screens have toolbars etc. from that one activity.
- Several ways to move from one fragment to next – view pager, multi pane etc.
- Activity has most data, so minimal inter-fragment communication needed. If still necessary, can be done via interfaces easily.
- Fragments do not need to be full screen, lots of flexibility in designing them.
- Fragments do not need to inflate layout if views are not necessary.
- Several activities can use the same fragment.
I know this was already discussed to death, but I’d like to add some more points:
Frags can be used to populate
Menus and can handle
MenuItemclicks on their own. Thus giving futher modulation options for your Activities. You can do ContextualActionBar stuff and so on without your Activity knowing about it and can basically decouple it from the basic stuff your Activity handles (Navigation/Settings/About).
A parent Frag with child Frags can give you further options to modulize your components. E.g. you can easily swap Frags around, put new Frags inside a Pager or remove them, rearrange them. All without your Activity knowing anything about it just focusing on the higher level stuff.
Fragments lives within the Activity and has:
- its own lifecycle
- its own layout
- its own child fragments and etc.
Think of Fragments as a sub activity of the main activity it belongs to, it cannot exist of its own and it can be called/reused again and again. Hope this helps 🙂
1.Purposes of using a fragment?
- Dealing with device form-factor differences.
- Passing information between app screens.
- User interface organization.
- Advanced UI metaphors.
A fragment lives inside an activity, while an activity lives by itself.