Fragments over activities for phones android - android

What are the advantages/disadvantages of using fragments over activities in an application when designing for a phone and tablet?
Should we have activities for phones and fragments for tablets. Is there any advantage of fragments for phones besides making the code reusable?
The android dev site http://developer.android.com/guide/components/fragments.html says on the phone, the fragments are separated into different activities. What then would be the point of using fragments?

1 & 2 what are the purposes of using a fragment & what are the advantages and disadvantages of using fragments compared to using activities/views/layouts ?
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;
The ActionBar. If you want tabs up there to navigate your app, you quickly see that ActionBar.TabListener inferface gives you a FragmentTransaction as an input argument to the onTabSelected method. You could probably ignore this, and do something else and clever, but you'd be working against the API, not with it.
The FragmentManager handles "back" for you in a very clever way. Back does not mean back to the last activity, like for regular activities. It meeans back to the previous fragment state.
You can use the cool ViewPager with a FragentPagerAdapter to create swipe interfaces. The FragmentPagerAdapter code 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 ActionBarSherlock and 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.
2
The best way to communicate between fragments are intents. When you press something in a Fragemnt you would typically call StartActivity() with data on it. The intent is passed on to all fragments of the activity you launch.
For more detailed understanding of Fragments check Link

Related

Proper Use Of Fragments

I want to know if I am properly using Fragments here...
My application contains 6 Activities. I wanted to add a Navigation Drawer to it so I am currently in the process of converting each of these Activities to Fragments. When I select an option from the Drawer, the appropriate Fragment will appear.
My Fragments are very "stand alone" meaning each Fragment is self contained and not interacting with other Fragments, for example a calendar that displays the date.
This would look the same on every device, whether a tablet or phone. EVERY tutorial I have seen about Fragments has multiple Fragments interacting with one another. Mine does not. My question is, from a design perspective, is it ok to use Fragments in this manner? Or should I just use Activities instead? Thank You.
If it is from the drawer, the proper design is to use fragments. You should be using Fragments whenever it's possible, as it makes your code easier to maintain and control. Communication between fragments are easier than communicating between activities.
In addition, activities are expensive to operate. Activities created also do not implicitly destroy previous activities.

Proper usage for Activities in Android

From what I understand, and activity is equivalent to a "page" in a web app.
For example, the list view would be one activity, edit view another activity, and add view a third activity.
While this works as expected, android activities seem to operate as individual apps--the action bar is different for each activity and so are the menus.
Is my usage of activities above correct or should I be using some other mechanism such as swapping out layouts and views?
Fragments are a core part of activities - not that much different. The use of fragments comes since Honeycomb 3.0 and the idea is an option to split the screen in several fragments at once. For example if you look at the gmail app for a tablet - you have one fragment on the left dealing with navigation and then the next fragment on the right is the list of emails.
On a mobile device, viewing area is limited, so you it could be said that fragments sort of behave like an activity - you interact with one fragment, which triggers another and so on and so fort. But in the end you always reference a super activity of each of these fragments - for example when you want to access the context.
So if you just want to wrap web pages in WebViews, stick with activities. If your scenario might involve developing for both tablets and phones, then go for the fragments.
Alternatively, you can read about the design philosophies of both here:
http://developer.android.com/guide/components/fragments.html
Good luck!
Fragments are to be used when you have common feature across multiple activities. From my perspective you should use individual activities as there will be back-end code to support (fetch and validate data, i.e. business logic). This way you have more modular code. Fragments is a new feature from v3.0.
From what I know, Fragments would be a good option to be able use different configurations/real estates on different devices. For example, if you are on a device with big real estate like Tablets or TVs you can display more content in a single activity, on the other hand for a devices with smaller real estate you can show content based on a progressive manner.
See this: http://developer.android.com/training/multiscreen/adaptui.html
Note that Fragments are supported only on devices running Android 3.0, so you might have to use Support Fragments (See: https://stackoverflow.com/a/6528757/713778)
But Again it depends on your specific needs. I am not sure what your exact use case is, but I would suggest you to watch some Android design in Action for some examples to improve your design and make your app "User Centric" (See: https://www.youtube.com/playlist?list=PLWz5rJ2EKKc8j2B95zGMb8muZvrIy-wcF)
I recently came across this https://www.youtube.com/playlist?list=PLWz5rJ2EKKc-riD21lnOjVYBqSkNII3_k seems provide an in-depth analysis on User Experience.
I would recommend using fragment for web like views. Fragments are pretty snappy compared to activities and reusable.
But before you start, make sure to fragments are good fit for your requirements since they are supported from android 3.0.
You can declare fragments from xml itself or you can create frame layout and add that view in the code itself.
http://www.c-sharpcorner.com/UploadFile/2fd686/fragments/
The above link has a good example tabhost.

TabActivity with ActivityGroup

I was able to get a TabActivity working properly with multiple Activity's using ActivityGroup. However, I noticed a weird behavior. When clicking on multiple screens in one tab, going to another tab and navigating back to the original tab where user was already navigating several screens, the ActivityGroup goes back to the main Activity rather than the last visited one.
I was following the tutorial here:
http://ericharlow.blogspot.com/2010/09/experience-multiple-android-activities.html
Is there a work around for the ActivityGroup to prevent this kind of behavior?
I think it is better to look for a tutorial on Fragments.
I know this is not really the answer to your question, but I think it would be better to immediately use Fragments. This because Fragments are now recommended for Android development, and should be used as much as possible in favor of older technologies. And the stuff you want to do, are the perfect example of things that should now be done with Fragments.
The tutorial you are pointing to is from 2010, so that means that the technology used there will be rather old (I haven't read it though, but I can imagine it will). It is best if you want to develop something, to immediately use the latest technology, in this case: Fragments.
Some information on Fragments

Why should I use fragment in Android?

For very long time, I think what is the reason of using fragment in Android if I just develop the application for Android Phone only but not 10.1.
Is it necessary to use fragment? Also, what is the usage of fragment,
I found that it may use for 'tab' and 'separate view'...
I really think it is very confusing. Can anyone explain briefly and give example?
From documentation
You can think of a fragment as a modular section of an activity, which
has its own lifecycle, receives its own input events, and which you
can add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
Some advantages are..
A particular UI part, once done in fragment, can be reused in
same/different activities.
You can separate different sections of UI, hence code will be neat,
and easy readable.
The ability of fragment to be able to reuse is very helpful when you are creating applications for different kind of android devices (phones, tablets). A well designed fragment can be just plugged into your UI hierarchy.
Fragments is a new concept introduced in 3.0 version.
The basic purpose of fragments is:
Fragments are designed to use the device UI space efficiently.
When you are writing an application in android, then some people can download it into phone, some into tablets. If you see the space in tablets it will be little bigger than phones. You should be able to use that space efficiently. But you can't keep writing different applications one targeting for phone, and other targeting for tablets. In order to do it efficiently, i.e writing only application that can fit well with all screen sizes, we use fragments concept.
fragments are designed as a reusable UI components between more than one activity.
Once you design a fragment, you can view it as a detachable independent unit, so that you can plug it into any activity where ever there is a space. That means you can reuse the code designed for a fragment.
Fragment you can think of it like a sub activity, which sits with in an activity and which contributes its own UI to the activity screen.
Fragments are always part of an activity. With out an activity, a fragment will not exist. So your fragment life cycle will always be affected by activity life cycle.
An activity can contain more than one fragment. Similarly a fragment can be re used in multiple activities.
If you use Fragment in your application, your apps will support all the device like small device, tablet and even google TV. In one .apk file, we will have different design for various devices.
This is the best Android tutorial that I've ever found. Section 21 covers fragments
Refer Here

Are android fragments like iframe in HTML?

I understand that android is a completely different thing than browser programming.
But coming from a web development background, I wanted to undertand what fragments are supposed to be used for ?
Are they similar to the concept of iframes in an HTML page having its own UI and can be added to any other page?
If not then what is tje general use-case of android fragments ?
They are reusable parts of your applications. You can have for example two different Activities using same Fragment. You may want to read Fragments Design Philosophy in the android documentation.
Here's also short description from the same documentation page:
A Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities. You
can think of a fragment as a modular section of an activity, which has
its own lifecycle, receives its own input events, and which you can
add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
Yes, I think you can think of them as the same at a conceptual level.

Categories

Resources