March 3, 2012

Fragment Tutorial part 1


What is a Fragment?

Before Android HoneyComb (API 11) user interfaces were built managing activities and views, but with the introduction of tablets and bigger displays there were enough room for what were once contained into two or more activities. Fragments were introduced to better handle that additional user interface area.

The official definition for Fragments is the following:
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. 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).
The very last part of the definition is a bit misleading. Instead of "sub activity", a Fragment is better defined as a "super view". In fact a Fragment is always inside an activity and behaves exactly like a View (can have setters, can generate events for its parent activity, is placed in the activity layout, and so on).


Why should I use Fragments?

Looking at current platform versions distribution, there are very few devices capable of natively support Fragments, they are more complex to use and conventional (pre-Fragment) user interface design is still completely supported by new devices, so why should I use Fragments now?

Simply put, it is an investment for the future. To be ready for time when mastering Fragments will be necessary to build a good app.

In order to be able to run Fragments also on pre-HoneyComb devices, a compatibility library called Support Package shall be imported in the project. This library provides compatibility with most of the new classes, so that you develop a single app and it runs everywhere.


When should I use Fragments?

It is difficult to give a simple rule for deciding when to use Fragments, but with some examples. In general if your app shall have a multi-pane layout, each pane should be implemented as a Fragment. But also for app with different layout for smartphone (single pane) and tablet (multi-pane), or with different layout for portrait (single-pane) and landscape (multi-pane).


Let's get ready for using Fragments with Support Package

As said before, if you want to use Fragments and provide compatibility through all Android versions, you have to implement them using the compatibility library (support package) in your project and build it.

  1. Locate Android SDK installation folder, and navigate to \extras\android\compatibility\v4 subfolder.
  2. Copy android-support-v4.jar in a folder named \libs under app root.
  3. Open app project in Eclipse, right-click on android-support-v4.jar and choose "Add to build path" in order to put the library in your app.

Now that Support Package is compiled in the project, you have to do the following when coding:

  1. When creating an Activity class that will contain Fragments, extend it from FragmentActivity instead of Activity. FragmentActivity provides Fragments support to pre-3.0 operating system versions.
  2. When creating a Fragment class extending it from Fragment, remember to import the compatible one (import android.support.v4.app.Fragment).


Fragment Tutorial - part 2 »

No comments:

Post a Comment