I have a similar technical problem I am trying to solve where I am using android's FragmentPagerAdapter to handle swipe gestures.
This is in regards to ICS...
My question is do you know or think that the android devs used fragments for switching between the groups, people, and favorites screens?
It seems like they are using listfragment
I am curious since I am considering making my fragments very content heavy and worry about performance and such.
Thank you
Fragments are relatively light weight components which can be replaced with other fragments on the fly. Even though there is no fixed guideline on how the app UI should be broken down into fragments i would suggest using a new fragment for every use case
So from using heavyweight fragments with the viewpager I feel comfortable with assuming that my original question is true. Either way fragments can be used effectively with tons of operations and they seem to work great.
Related
I can't seem to find an answer for that. How many fragments can an app have, or how many xml layouts in general, before it starts getting cluttered and slow? All I found was that with too many nested layouts the activity itself performs worse.
Yes, theoretically it can. But it's not the number of fragments which can make an app slow, it's the way you use them. Even 2 fragments, if badly used, can make an app slow. On the other hand, tens of fragments could be handled fine. If your app instead needs 50, or 100 fragments, unless it's a really complex app and you're on top of it, then it's a good indicator that you're doing something wrong, either in the app flow, or the design. Android Studio provides you very good tools for profiling an app, use them, see where your bottlenecks are, and fix them. Measure the improvements before and after the fix.
No, there is no limit on making any number of fragments in Android app. And it does not harm any app if you make hundreds of fragments. But the way you are using those it DOES MATTER. As far as the matter of nested layout is concerned, yes it all depends upon your hierarchical level. Suitable approach should be used. Obviously not all layouts you will be showing in your activity. On depends or in certain conditions you will be using different nested layout. If this is the case then you can use fragment for dynamically update the UI or the Activity or Secondly you can dynamically add the views in your activity on demand. All at once if you are going to show complex nested layouts and those are in deep as well, this can cause sometimes some jerk or flick to load.To overcome this, You need to first think about weather it is necessary to load all the views else load on demand. Hope that helps you.
All the reasons I can find for using Fragments in Android activities have to do with having the ability to display multiple classes/view in the same screen, encapsulating multiple logical components, etc.
Considering all this, it seems, fragments are only actually useful when you employ the use of many of them.
Is that so? Is there ever a point of using just one fragment in an activity?
I ask now because I saw an option on Android Studio to do just that, and I am wondering what the point is.
Out of my personal opinion, I would say yes.
For the following reasons:
Assuming you are familiar with Fragments, creating a Fragment is hardly any extra work plus has the following benefits
Fragments can easily be reused somewhere else (possibly another Activity that has more Fragments, furthermore, Fragments do not necessarily need to use up the full screen).
Activity transitions are more expensive, Fragment transitions are more sophisticated.
The Fragment animation framework is better (in terms of usability and performance).
I always like to keep the number of Activities to a minimum which keeps the AndroidManifest.xml short and clean.
UI separated into Fragments leads to cleaner code structure and easier code maintenance.
According to google coding guidelines, it is best practice to create as few Activities as possible, and create multiple Fragments instead that are switched inside an Activity.
Well it depends, if you are going to use that fragment in another activity yea, you have a "point" and maybe in a future you can reuse it on another activity, but in the case for example of a splash screen well, it don't have a point. All depend in the uses you want to give to your application.
Pros:
-> reusable piece of code
easy to utilize it again in any module
easy to debug
-> handles orientation changes better than activity using setRetainInstance(true)
-> great help when scale the app in future for multipane layouts or multi-screen support
Cons:
-> little overhead and time consuming if you are not familiar with fragments
I have read the documentation and some other questions' threads about this topic and I don't really feel convinced; I don't see clearly the limits of use of this technique.
Fragments are now seen as a Best Practice; every Activity should be basically a support for one or more Fragments and not call a layout directly.
Fragments are created in order to:
allow the Activity to use many fragments, to change between them, to reuse these units... ==> the Fragment is totally dependent to the Context of an activity , so if I need something generic that I can reuse and handle in many Activities, I can create my own custom layouts or Views ... I will not care about this additional Complexity Developing Layer that fragments would add.
a better handling to different resolution ==> OK for tablets/phones in case of long process that we can show two (or more) fragments in the same Activity in Tablets, and one by one in phones. But why would I use fragments always ?
handling callbacks to navigate between Fragments (i.e: if the user is Logged-in I show a fragment else I show another fragment). ===> Just try to see how many bugs facebook SDK Log-in have because of this, to understand that it is really (?) ...
considering that an Android Application is based on Activities... Adding another life cycles in the Activity would be better to design an Application... I mean the modules, the scenarios, the data management and the connectivity would be better designed, in that way. ===> This is an answer of someone who's used to see the Android SDK and Android Framework with a Fragments vision. I don't think it's wrong, but I am not sure it will give good results... And it is really abstract...
====> Why would I complicate my life, coding more, in using them always? else, why is it a best practice if it's just a tool for some cases? what are these cases?
I am sorry if I wrote too much, and thanks for your time. I hope I will get your attention, because I really need ideas and experiences about this topic.
Best regards, Ahmed
You shouldn't always use fragments. Fragments have their uses, such as when you want to page in and out parts of the screen or when you want to drastically change the UI in different orientations. When they make sense, use them. When they don't, skip them. I find they make sense in maybe about 10-20% of apps- I rarely see the need.
If there's a certain positive aspect apart from the simpler reuse of logic through different layouts, it's the ability of Fragments to be kept alive by the system at orientation change, aka while an Activity is reconstructed from zero, a Fragment can retain its instance, and therefore using them is more stable than an Activity. Also, switching between Fragments is quicker.
Personally, if I don't need to mess around with different orientations and layout sizes, I still prefer using Fragments and a singular container Activity around it, for stability and seamless switching between the different screens.
Its quite a general question and not directly related to a specific programming problem. But in my opinion good software is based on good design and therefore a good understanding and best practices. So your question is a good one for stackoverflow.
So, what about fragments. It took me a while to understand why you could or even should use them. As #pskink said, you can easily live without them. But if you are planning to rollout your software on different devices, you should definately think about fragments.
The screen resolution and density is not the only problem. Think about a smartphone. The screen is much smaller, so you can not present your app the same way as you can on a tablet. For instance a master detail flow. Left side, a list of elements and when you click one element, you will see details of that element on the right side. Easy to do on a tablet. But on a smartphone you would put the master-view into one fragment and the detail-view into another one.
You got two options to realize that scenario. Either programm different activities for smartphone and tablet but because they are actually doing the same logic, it's better practice to put the logic into fragments and reuse those fragments in two layouts (phone/tablet).
I read the fragments tutorial, but I still don't understand why they are actually needed. The tutorial gives the example of the 2 fragments in a wide screen and 2 activities in a small one, but I actually could just use a view and put it in the same activity or in another activity to achieve the same effect, so what does a fragment give me that a simple view doesn't? Thanks.
A fragment has a life cycle of its own , so you don't have to worry about memory and objects in larger screens where this does matter.
They're suitable when you want to put different content for different types of layouts. Mainly for building an app that's suited to both tablets and phones.
Think of a Fragment like a different activity within the same screen. Sometimes it's easier to have the code itself to be controlled within the fragment, rather than in a master Activity, especially if you intend to split them into separate layouts.
Things like Fragment dialogs are also more powerful than classic Dialogs. Communicating information to a fragment is a little easier and more efficient than between activities (though this may vary according to situation).
If you don't have a reason to use them or don't feel like experimenting, go as simple as possible. There's quite a bit of overhead to Fragments, so unless you're designing for multiple layouts (mainly tablets), it's more work for little gain.
I've faced with a question if it's necessary to rewrite my application for using Fragments API. The application contains only Activities, every of which is responsible for one screen only and will not contain two ore more fragments on one screen as in the SDK example. Should I rewrite the application to Fragments API?
In the case if I must rewrite, could you please tell me which structure should be used in the project:
Each Activity will contain one Fragment and manage it.
There is only one activity and all the other Activities should be rewritten to Fragments.
In general it`s not clear when the Fragment should be used.
Is it necessary to rewrite my application with Fragments?
Implementing Fragments in this case is just extra work. If you can get the effect you want using only Activitys, that's perfectly fine.
That said, implementing your application using Fragments even when its not necessary isn't always a bad idea, as it makes transitioning to multiscreen layouts quite simple. If you can see yourself optimizing for tablets at some point in the future, implementing your UIs with Fragments might be a good idea.
Here is a quote from the following link:
Android introduced fragments in Android 3.0 (API level 11), primarily to support more dynamic and flexible UI designs on large screens, such as tablets. Because a tablet's screen is much larger than that of a handset, there's more room to combine and interchange UI components. Fragments allow such designs without the need for you to manage complex changes to the view hierarchy.
http://developer.android.com/guide/components/fragments.html
It doesn't sound like your app design necessitates the use of fragments. Here is an example of UI design that fragments were intended:
Hope this helps!