17

Are there any patterns on how to handle UI Transitions in Android Activities vs Fragments? I am currently looking into a UI that has at most 3 columns in Landscape. I would like the UI to start with 1 column all the way across the screen and then on selection of something move in the second column and then on clicking on something in the second fade in the 3rd on tablets and phones and fade out the 1st column on phones. I am wondering when I should do this as an Activity transition and when I should just use Fragments with Views that Appear. As far as I have read fragments can be moved over to other activities so my choice is either implement Activities with static column layouts that then transition taking the fragments with them or have one Activity with all 3 columns and have the Activity manage the Appearing of the Fragments. Both approaches could work but I was interested in pros and cons from as many angles for both solutions.

There are two questions similar to what I am asking but don't quite answer mine

Community
  • 1
  • 1
AGrunewald
  • 1,735
  • 3
  • 16
  • 25

3 Answers3

12

Fragments can seem like more code up front (since you're putting a view in a fragment, and a fragment in an Activity, instead of just a view in an Activity), but they're great at saving you from headaches in just this kind of situation- Definitely go with Fragments. They even handle the transitions for you.

We have some sample code called "Honeycomb Gallery" you can take a look at here, which has a two-column-plus-actionbar layout, and the ability to show/hide the leftmost column. This should give you a good head start in figuring out how to do layout for multiple fragments and show/hide them.

FYI, one important trade-off to using multiple fragments in an Activity instead of multiple Activities, is that fragments don't directly respond to intents - For instance, if you had a note-taking app where "View Note" page was an Activity, and you changed it so that there was a "view note" Fragment inside the main Activity, then you'd have to set it up such that the main Activity received a note ID AND a note action (create, view, edit, whatever) in the Intent, as opposed to just having the "view note" activity receive the note ID in the Intent. The main Activity would then need to set up the fragments on the page accordingly. Not a huge deal, but if external accessibility to various parts of your application via Intent is important, then it might be easier to break your app out into a few Activities, as well as use fragments to represent the individual components.

Alexander Lucas
  • 22,171
  • 3
  • 46
  • 43
  • Thanks Alexander that is a good pointer, I actually had forgotten about that Sample app. To me it is less about Activites vs Fragments and more about when to use one Activity containg static Fragments and when to dynamically load the Fragments. I think one good pattern out outline is the Intent mechanism. So if you want the screen to be addressable via an Intent then have it in it's own Activity. Can you think of other patterns? Will the Ice Cream Sandwich Core apps have good patterns in them that one can use to study how to use Fragments best? – AGrunewald Oct 21 '11 at 02:40
  • 1
    At a recent Honeycomb Android Dev Lab, we had one dev say they go by "what would gmail do". While what works in your app is always most important, they do raise a good point in that the gmail app (both tablet and phone) is a very solid reference in terms of good use of fragments, and updating both Action Bar items and visible fragments based on context (i.e, actionbar items change based on whether you're reading an email, writing one, or browsing a list of emails.) In Honeycomb, Youtube & Contacts, also awesome. Per ICS, I can't talk about details just yet :) – Alexander Lucas Oct 21 '11 at 21:03
  • Thanks for the great update Alexander. I am looking forward to the ICS Open Source Drop to have a look at the code. I know many of the Google Apps aren't included but I believe there are still enough core apps in the open source tree to learn a lot. – AGrunewald Oct 26 '11 at 07:23
1

Based on the page The Android 3.0 Fragments API, an Activity is stand alone while a fragment can be though of as as a mini-Activity, which must be hosted within an actual Activity.

It goes on to say that the introduction of the Fragment API gave the android developers the opportunity to address many of the pain points developers hit with Activities, so in Android 3.0 the utility of Fragment extends far beyond just adjusting for different screens:

I think that using a single activity for an app is not necessarily a wrong decision, just a matter of style. It is a decision that you should make based on what you are trying to accomplish.

However, the introduction of Fragments was seen to solve real world problems. Based on that alone, I would recommend that you writing some "Proof of Concept" code and evaluate the results. At this time, this may be the only real world test that will matter

Noah
  • 15,080
  • 13
  • 104
  • 148
  • Thanks for the comment Noah, I agree with what you are saying, unfortunately it doesn't answer my question since I was looking for patterns. But it seems we are all still breaking new ground with Fragments. – AGrunewald Oct 21 '11 at 02:45
1

Use Activities for Full Screen

Use Fragments for Part of or no Screen (but not a service)

In my main application, there is on-screen tabs in a horizontal scroll-view I wanted to persist across multiple sections of the app. Sections include News,Photos,Videos,Schedule etc. All single-user focusable tasks.

The main Application that houses it all is a application, and the tabs are just a view which call the fragment Manager.

However, I use Activities for complicated user activities deeper in the application. E.g. if someone plays a video, views a item detail page and the photo-gallery/slideshow sections, because they are all full screen components.

There is no need to show/hide fragments when transitioning to full screen because the activity stack handles everything you want to do it quickly and easily, and keep your code minimal and clean.

So I have Activity -> houses fragments -> launch full screen Activities for special commands.

HaMMeReD
  • 2,440
  • 22
  • 29
  • Thanks for the Answer HaMMeReD I knew this much already :-) The question was more directed at when to transition between activites to change the screen and when to just change dynamic fragments. – AGrunewald Oct 21 '11 at 02:47
  • Use Activities every time it's a full screen component. You can re-use fragments in multiple activities. – HaMMeReD Oct 21 '11 at 06:01
  • Actually, what makes a full-screen component on a phone is a part-screen component on a tablet, so it still makes sense to put "full screen" bits in fragments instead of activities. – Alexander Lucas Oct 21 '11 at 23:35
  • I'd like to point out that Fragments within Fragments are not supported by the API and they will cause crashes. So I still wouldn't encourage to use Fragments for full screen applications, if you intend to use fragments within them. – HaMMeReD Oct 23 '11 at 23:31
  • Really every activity should be populated by Fragments. You should never have an activity with UI controls, only an activity with fragments. If it is a full-screen activity, it should contain a single fragment. – mjsalinger Nov 19 '12 at 22:32