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
Related
I've read the official documentation with regards to designing apps for both phone and tablet here:
http://developer.android.com/guide/practices/screens_support.html
However, I'm unable to relate this to my use-case. My app is made of 4 main screens, all accessed using a view-pager which only works on fragments and as such, I have 4 top level fragments embedded inside my main FragmentActivity which are arranged in a tab layout structure with a view pager.
Question is: would it be possible to somehow achieve the different design for phone/tablet using fragments instead of activities? Based on the link above, if I was using fragments instead of activities for my top level items, it would be simple since I would simply instantiate a different layout with the same fragments but from what I understand, up until very recently, fragments were not able to include other fragments in them and since I would like my app to be accessible for older phones (I'm targeting SKD v14), I don't think I'm able to achieve this using different fragment layouts and composite fragments.
Ideally, I would love to be able to have the same code base and use the same fragments for both tablets and phones, albeit define separate layouts for each device, but I'm not sure on how to go about it.
Many thanks in advance,
you can display two fragments simultaneously on tablet whereas on phone you can show one fragment at a time-that is the main flexibility fragments provide.refer gmail app screenshots for phone and tablet and you will get better idea about what I am talking about-https://play.google.com/store/apps/details?id=com.google.android.gm.
If you dont have similar UI flow as gmail(click on UI element in one fragment to open other fragment to show details) I dont think using fragments will provide much benefits.
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.
I’m in the process of implementing a new version of an app and want it to move towards using fragments and thereby enabling it to be used on tablet devices.
I have read several approaches (e.g. How many Activities vs Fragments?) and the guidelines on developer.android.com (http://developer.android.com/guide/practices/tablets-and-handsets.html).
The Android Guide aims for one monolithic Activity for Tablets that deals with replacing fragments as needed whereas the handheld version does it by using multiple activities.
See example: http://developer.android.com/images/fundamentals/fragments.png
Why are these two approaches not combined?
Let’s say FragmentA from the above image is the menu of my application where I can select a different aspect and FragmentB is the corresponding Content for the selected item.
Tablet devices
If my application can be separated into different aspects that correspond to the items in the menu, wouldn’t it make more sense to encapsulate the functionality behind each menu item inside a different Activity instead of using one monolithic one?
Selecting an item in the menu would exchange Activity A by ActivityB/C/D/E. Whereas those are configured in the same way, in the sense that is has a menuFragment on the left and its content fragment on the right. Interactions in the contentFragment would replace it with different fragments and so on. But the functionality would be covered by the specific Activity that holds it. So I would not have one monolithic Activity, but smaller and more specific ones.
Handheld devices
Similar to the above, instead of using a different activity per ”screen” I could reuse ActivityA/B/C/D/E and their contentFragment in a singlePane-Layout by replacing it accordingly by specifying the singlePane-layout for handheld devices. One Activity manages one task with several fragments.
So I would end up with the same number of Activities for tablets and handheld devices that are configured the way to handle both cases a bit different. Activities would be neither gigantic and full of logic nor totally dumbed down, but somewhere in between.
Is there something wrong with this approach?
I’m currently thinking about how to move forward and this approach seems reasonable to me, but maybe there are things I have not considered that make it not the best choice.
I am developing a forum reader, which I expect that it will be run on phone only, not tablet.
The application originally have 3 views, and each is assigned with an Activity.
For example, if the user select a board on "Board selection screen", an Activity will start, and show the thread list of the selected board to the user.
Recently I have learned how to use fragment in an Android application. therefore I decided to convert my application from an Activity based apps to a Fragment based apps.
Although the apps works as fine as usual after the change, I encountered a problem in saving the state of the fragment. I failed to find the event to let me save the state of fragment (When I am switching from A fragment to B fragment). The most likely event is onSaveInstanceState(Bundle), but according to the API guide, it will only be called when an activity is about to be stopped, and it is not my case.
Actually I have tried to save the state in getArgument() in onDestroyView(). Nevertheless, according to the API guide, the values in getArgument() should only be used for instancing a fragment. Although everything work normal when I am saving the state in getArgument(), I believe that I am doing it wrong.
I have tried to do some googling about Fragment. I found that most of the artist suggest that Fragment is useful when an application is expected to run on both phone and tablet. And I don't find any artist that is talking about how to do screen switching between few Fragment within an Activity. I started to doubt that Fragment are only useful for supporting different device, but not for screen switching.
Sorry for the above junks. My questions are, does Fragment is not very useful when I am developing an application which for phone only? Should I change my application back to an Activity-based apps if I don't expect this to run on tablet?
One more thing Fragments are good at is they are good for Dialogs. Fragment-based dialogs have none of the problems you usually have with showDialog() when rotating the phone
No, you should stick with Fragments. They're incredibly versatile and will make your life easier down the road if you decide to rearrange things. As far as saving state, you just need to override onSaveInstanceState() in your Fragment (not the Activity). This will absolutely be called when switching between Fragments -- I use this all the time.
If we're talking about "just work" you can just go ahead and use fragments or just normal activities and layouts. However, what fragments provide, apart of scalability when developing for other devices like tablets, is that it's giving you more code separation, which is so, so great for the maintenance of the app.
As Android documentation states: "An activity is a single, focused thing that the user can do."
However with Fragments we will be able to do many "things" within the same Activity as Reto Meier suggest. His suggestion is to replace a selection fragment by a content fragment within the same Activity (section "Within our code this produces a dilemma").
Lets say my application is a "bit" more complex, with many activities, with a complex navigation tree and designed with the "single, focused thing that the user can do" principle in mind.
Lets say now I have to adapt it to Fragments and large screens... and that I don't want to create a second application, neither have two completely different logics (one for phones other for tables) inside one application.
Should I have one Activity to manage all the application fragments and fragment transactions? Like Retro Meier suggest above. Is that the recommended path to follow? Thus breaking with the "single, focused thing that the user can do" principle for Activities?
Or am I missing something? I hope ;)
BTW, I think Fragments looks great, but from what I have seen till now, only if you are creating an application from the scratch. Because making applications to be compatible with phone and tablet looks like going to be a bit tedious. Hope to be wrong :)
Dianne Hackborn already has answered (thx for the link mgv):
you could put your entire application in one activity in which you change the fragment structure as its state changes
So then Activity becomes a sort of container where you will be able to plug Fragments. I like the approach, but... in my app there are about 30 different operations available, each one requires about 2 to 4 screens steps to be performed(forms and selection lists), all of them differ and there are also navigation restrictions. It works fine with Activities each one handling one screen/step behaviour.
So then to port to Fragments I should move each screen logic to Fragments and use Activities as containers for each operation. Thus leaving Activities as the ones managing the navigation between Fragments for every operation, right? Looks like going to be a pain to adapt long applications. :(
Current Activity definition should change a bit btw. :)
Should I have one Activity to manage all the application fragments and fragment transactions?
That is impossible to answer in the abstract. However, most applications will have multiple activities, even in a fragment-based world. Some of that will be to accommodate smaller screen sizes, where it will tend to be one fragment per activity. Some of that will be required by the framework (e.g., inheriting from PreferenceActivity). And, some of that will be by GUI design.
Thus breaking with the "single, focused thing that the user can do" principle for Activities?
That portion of the documentation was written in 2008, perhaps earlier. Had fragments existed back then, I imagine the documentation would state that a fragment is a "single, focused thing that the user can do", with activities serving as an orchestration layer, determining what fragments are visible in what circumstances.
The documentation will not in all places be updated to reflect fragments, and even if it does, it will take some time. For the balance of 2011, at minimum, you will need to perform your own translations of 2008-era instructions to convert them to 2011-era fragment-based UIs.
Lets say now I have to adapt it to Fragments and large screens... and that I don't want to create a second application, neither have two completely different logics (one for phones other for tables) inside one application.
I have no idea what you consider "completely different logics" to be. In a fragment-based app, most of your business logic will be in the fragments themselves. The activities, again, serve as an orchestration layer, determining what fragments should be visible and coordinating event handling. The latter will get a bit more complicated than it used to be, since sometimes clicking on an item in a list will bring up a new fragment and sometimes clicking on an item in a list will start a new activity, depending on screen size.
Or am I missing something?
To be honest, you are missing enough concreteness to your question to make it reasonably answerable.