Should each Fragment have its own Activity? - android

Android Studio 0.8.10
I have developed an App that has 3 fragments. I have just used 1 Activity and when I want to display a different fragment I just replace the existing fragment with the one I want to display. However, as I have 3 fragments now, and maybe more in the future, I think this will get harder to manage.
I am just wondering what is the design pattern when programming with multiple fragments, should each fragment have its own activity?
I will be scaling this to Tablets in the future, so I am not sure what impact this will have if I stick with the multiple fragments and single activity.
Many thanks for any suggestions,

should each fragment have its own activity?
Yes, but you can also use nested fragments.
I think this will get harder to manage.
you are right but
i think you must match your app with some other widget for example if you have multiple fragments that want to show one after the other use viewpager or you can use horizontalscrollview. you can create tabs and sync them by viewpager and so on.

Yeah, this can be really hard to figure out. I think a pretty good analogy, from the web application world, might be a servlet and a frame.
An Activity is like a servlet. It is one page in your app's workflow.
A Fragment, on the other hand, is like a block of content. It might appear in several different contexts and it might be served by several different servlets.
In MVC terms, the activity is largely part of the controller. A fragment, on the other hand, is more like a view include.
Much of the time, those two concepts align. A page in the workflow frequently contains exactly a single block of content. As you have, wisely, noticed, though, when you get more screen real estate (on a tablet), it is entirely possible that a single activity will display more than one fragment.
A single activity, on a tablet, might show, for instance, both a list of selectable items, and the details for the currently selected item in that list. When you have less space on the screen, though, those two things would be displayed as separate workflow items. Clicking on an item in the list invokes an entirely new activity.
The content is constant. The workflow changes.
Most modern applications will use a Fragment to display Activity content. It makes the application more flexible and easier to adapt to wildly different screens.

Related

Nested Fragments bad practice?

I am creating my app. I am trying to follow all google's guidelines. There is a great part of UI - Fragment. It is really great thing that makes UI smoother and prettier.
Of course it is better to split my screen into separate logic portions of UI which can be later reused in another activity, layout whatever...
Fragments are more lightweight than Activities. Animation between fragments is smoother and looks better.
All that is great. What about using one Activity per app ?
According to the Eric Burke You have to use Fragment whenever you can do this. Here is the lecture - Android App Anatomy.
Surely, using one Activity per full app can bring some benefits.
But of course there are some cons.
Let's consider simple app, it is not real app ,but just for example.
Here are three screens.
It is not exactly the same UI as in my app, just for make it easier to understand my question.
There several ways we can follow to build such UI.
Each screen is single activity with it's own layout.
Each screen is again single activity but all portions are fragments, for instance on the first screen. It will be three fragments : ViewPager, Horizontal List and Custom View. Second screen will have only one Recycler View fragment and so on.
Use on activity for all screens. In this case we also have several ways.
a) Use one container Fragment for whole screen and all widgets will be the part of one fragment layout.
b) Use one container Fragment but with nested fragments.
c) Use fragments without container and replace them all or some of them when we need to change UI, for example to change UI from first screen to the second we need to delete all fragments from the first screen and add one new fragment (list view), because we don't have the same parts of UI on these two screens.
All in all, I cannot decide for myself what and when to use, what is better according to the current guidelines, what can bring user better experience.
I am worrying about nested fragments, but if there was a bad practice, I think, google wouldn't add such feature into the framework. So may there is acceptable way.
I want to understand where it is better use Activity,Fragment or some mixture of them. There is no problem to write code for all this cases, but the main goal is to follow the best practices in building software architecture.
I will be really grateful for anyone who can help to understand this topic.
Thanks everyone who have read this to the end and those who can help me with this question.

How and when to use Fragments in applications?

I am very in doubt of the structuring of my application. When to use Fragments is a big question for me still. I understand the concept of fragment, but I would like to know how more experienced programmers use them. Is it really only when there is a specific task the fragment should do?
For instance, POP-UPs, what layout would be better used?
And back to the fragments, do you usually have a skelleton fragment that can be used for more than one thing, and then reshape it to different final forms (minor changes) or would you just use different fragments if the layout changes?
You should use fragments in a few situations but its going to depend on your app really.
If you have a layout that is going to be used in multiple places and the code is relatively the same, that is a very good candidate for a fragment so you can keep code down.
If you are making an app where the layout changes based on orientation or device type (tablet vs phone) then fragments are highly recommended to hold different layouts. It makes it easier to change or show multiple layouts if need be on a tablet.
I'm sure there are more scenarios but I would say these are the basics for fragment decision
The most common way I use them is as a kind of plugin layout for my activities. Let's say I have several activities and all of them need to display an address, phone number and some buttons (call number, launch navigation/gps, etc). I pass the fragment the id and the fragment does the legwork of grabbing the information and populating fields for my activities. This way, if I need to add anything, say an email address, I just need to modify one section of code rather than each activity and layout file.

Why use two activities and two fragments when one activity suffices

This is the image explaining the usage of fragments. The first image shows two fragments and two activities.
Lame doubt. Why use two activities when the sole concept of using fragments is err.. using fragments instead of switching activities.
Depending on your goals you can do it either way.
The method shown in the guide can be implemented entirely in XML layout files so it is a better method to teach to a new user of fragments.
The method you suggest requires the developer to manage fragment transactions in code, which is not too difficult, but why do it if your app does not have any special behavior that requires the extra work.
Also, since the animated transitions between fragments look different than activity transitions, your method will reveal the use of fragments at the user level. The method in the guide uses fragments as a modular programming technique that is transparent to the user.
You end up with an app that uses available space on all device types, but on a small device it acts just like a classic app that users already understand.
The idea is that when you have extra room (such as on a tablet), you can display the content from what would have been two activities side-by-side rather than as two separate activities.
Think about a mail application. On a phone, you fn really only fit the list of mail on a screen, and you click on one to open the content of that mail on another screen.
If you did that on a tablet, there's a huge amount of wasted space; you can display the list of mail on the left side of the screen, and the selected mail's contents on the right side.
Because the list UI is the same in both examples, and the mail-display UI is the same in both as well, you can reuse that UI by including them as fragments. The logic for those UIs is also self-contained in the corresponding Fragment classes.
This allows the user to see more content with fewer activity switches.

Are Fragments and Fragment Activities inherently faster than Activities?

Are Fragments and Fragment Activities inherently faster than Activities?
If I don't need to load my activity in fragments, should I be using FragmentActivities and Fragments over Activities?
Reason I am asking is because I have been using Activities, exclusively, for years, and the Facebook SDK as well as Google Maps 2.0 have forced me to use Fragments, and I wonder now if they are inherently "better" or not, versus some other implementation.
If this "not constructive" or "too open ended" then obviously the answer is "no". But if there are some Google developer documents or blog on this exact subject, then I would like to be aware of it
I became a believer in Fragments in my last application. Whether or not they are computationally faster, they feel faster because you can swap them in and out basically instantaneously, including full support for the back stack if you do it right (call addToBackStack() on the transaction, or something very similar).
I now use Fragments / Fragment activity for all navigation I want to feel very quick, like clicking on a row to get more details. I only launch new activities for when I want to do a fundamentally different thing and have a clean slate to work with. For instance, I usually have a LoginActivity that deals exclusively with logins/registrations, and at least one more that is the core of the app.
But the fundamental benefit of Fragments still remains their flexibility. I can show fragments on top of other fragments, re-arrange them on different screen sizes, etc. But there are loads of other benefits. It just takes a while to feel natural (just like Activities did at first).
One caveat, I always regret embedding fragments in my layouts. I can't give exact reasons here off the top of my head, but essentially you just lose some flexibility. Instead, I build a normal layout for each fragment, and add a placeholder view in the activity layout, create the fragment programmatically, and use transaction.replace() to add it to the layout. Perhaps because this is the main way I swap fragments in and out of that placeholder view, and prefer to just have a single way of doing things where possible.
yeah, fragments are introduced exclusively for supporting large screens to use the area efficiently.handling fragments is very easy and in terms of memory.but nested fragments makes trouble
Fragments are very useful if you want to split a screen. So you can have different views within the same screen. Another way of using fragments, lets say you've got tabs to categorise items. Could have clothes, shoes as your tabs. Each tab will have a fragment to hold the products. The tabs could either held in activity or a fragment. I do find fragments a slightly faster than activities but really its not something you would really notice in most cases. Regardless if they was intended for speed or not they still seem/feel little quicker.
The downside of using fragments, is certain callbacks like onBackPressed is only in an activity. Fragments has no access to this. I often find its best to minimise how much activities as possible. Also don't forget Activities aren't just views, they're also a screen. Whereas fragment is only a view and doesn't have a screen. tool/action bars etc. are also only for Activities however if your using a custom toolbar, you can use this in a fragment by implementing onTouch(if not button but some object) or onClick(buttons) the method for these will give you what you need. So really there are some drawbacks but there is almost a workaround for at least some of them.
I do agree Fragment transition is awesome and pop the stack when working with back buttons and onBackPressed Does the trick.
I always use a switch in parent activity etc. to see, which fragment needs to be in view, i often update it using interface passing bundle etc. Not sure if anyone else has found a more efficient way or not. But I do find it really useful when switching views.
Yep fragments are top brass for most things.

To fragment or not to fragment?

As I've started to adopt Fragments more and better but also as Fragments functionality is increased (Fragments in Fragments, MapFragments) I'm starting to reach a point where I need to define when I should make a new View/Action as a Fragment or as an Activity?
An Activity is defined as:
An activity is a single, focused thing that the user can do.
But a Fragments have kinda taken that definition instead as described in the docs:
For example, a news application can use one fragment to show a list of
articles on the left and another fragment to display an article on the
right—both fragments appear in one activity
This is two things the user can do in one Activity with two Fragments.
So I'd like some input/help to figure out what is the best approach to decide if I should make a new action/view as a Fragment or as an Activity?
The answer depends on you and your development practices (or those of your company). However, my opinion is this: At a minimum, if you think the functionality being developed could be used within multiple Activities, or if it could ever be used in an Activity alongside another view (as on a tablet), then you should make it a Fragment.
We've recently adopted the philosophy of creating Fragments in all cases. Our Activities are now just top level coordinators, basically the glue that brings things together. This makes for a consistent and flexible architecture. This is important to us as we have numerous engineers at a couple of locations working on code.
An Activity is defined as: "An activity is a single, focused thing that the user can do"
That is more an issue of dated documentation than anything else. Activity has that same definition... when we are on a smaller screen size (e.g., phone). As you move up to larger screens, the odds of an activity being more complex than "a single, focused thing" increases.
So I'd like some input/help to figure out what is the best approach to decide if I should make a new action/view as a Fragment or as an Activity?
Here is my general heuristic:
If you anticipate that such-and-so piece of UI might exist standalone on a phone-sized screen, but be used in tandem with something else on a tablet-sized screen, make it a fragment.
If you anticipate that such-and-so piece of UI will always exist standalone, just create a simple activity.
If you anticipate that your ability to anticipate is not that good, err on the side of making more fragments. For example, you might say, "well, help will never need to be alongside anything else" and make it be an activity. Then, if you realize that other pieces of UI might benefit from the help being side-by-side with them rather than off on its own -- so the user can read the docs and perform the actions at the same time -- you will regret not having made help be a fragment, as you will have to do some re-work.
If such-and-so piece of UI would never exist standalone -- in other words, if it is more like a single widget than a full activity -- and you anticipate using it on multiple projects, make it be a single widget, in the form of a custom View or ViewGroup.
But, as jsmith indicates, there is no universal right or wrong answer. BTW, AFAIAC, jsmith's answer is the correct one here, but I was going to be way too wordy for a comment on his answer... :-)
I've been developing in Android since 1.5 so I have been developing from quite some time Activities and recently Fragments.
Quite frequently fragments left me with a sour taste in my mouth... an example was when I needed a kind of paginated Dashboard with buttons. For that I used a ViewPager + 1 fragment per button. I had all kind of problems because before Android 4.2 fragments couldn't be nested.
Another problem was the asynchronous mode of function of the fragments that when the needed to be moved from one place to the other quite rapidly it had all kind of misbehaviours.
Don't think that all was bad... in more simple cases, the use of fragments worked quite nicely.
So, in my opinion, whenever you have an area that is self-contained, that isn't moved frequently on the views, that can be reused in several screens and also you support tablets (or my in the future), use it.
If you need nested fragments, views that are re-arranged quite frequently, or code that will not be reused, don't.

Categories

Resources