Thursday, 9 August 2012

Android Fragments

I've been developing in Android for the last 5 months. Aside from the normal learning curve when learning a new technology, my team and I have found Android's fragments a bit of a headache. The android documentation is great. But there are a few things they don't explicitly mention that I really would have found useful to know or think about up front.

There are two options for using fragments, really. At least when looking at it from a high level. And I didn't realize the benefits or drawbacks of either of these... or the implications.

Option 1- Implement it and manage it yourself. 

This is what I did first. In this sense it's still a sort of miniature activity. 
There are a few ways you can do this.

  1. Define it as a fragment in the layout that your activity inflates.
  2. In doing this, you can inject it into your activity (if you're using roboguice) or pull it out into your activity as you would any other layout component to operate on it or delegate to it.
  3. Use the fragment manager to load the fragment in a FragmentTransaction and then load it into your layout based on some id (within a LinearLayout or something).
This would allow you to toggle the fragment for a given section of your activities layout.

The first way I went was a little of both of these. As it seemed to fit the bill and I didn't know any betting. I have since found that managing many fragments within an activity in the above fashion does not scale well. Also, the thing mentioned in the first option... where you can grab your fragment from the activity and delegate to it. That turns out to also be a mess. Ideally, you want your fragment to live in it's own little world. Without having to talk to it's activity or whoever is holding it.

Option 2 - Use Android components 

(i.e. ActionBar, Menus, etc..) 

What happened in my situation was that the number of fragments I needed on my screen was growing. There was more and more stuff going on and now I needed to manage the interactions myself. There was realization at some point that... 'Hey, those Android docs going on and on about the Android way of doing things... maybe they can help here!' So enter a few useful components.

Action bars and menus manage Fragments for you! You just hook into the behavior they provide and tell them which Fragment and when. It also allows for an experience your user is used to from other Android apps.

This is pretty much the code used in the Android API demos code. But you get the idea...


It's much easier to keep the Fragment separated from the Activity. Thus decoupling and simplifying. For more code I suggest to see the API demos. They're very useful.


As normal, it really depends on what you want to do. If you have a complex interaction between many different components on your screen, I really feel now that using the suggested Android approaches is easiest. If you just need one little fragment... going with the hook it up yourself approach may be best. But knowing that these are the two option paths may help in making the decision.

No comments:

Post a Comment