_**Prerequisite**: You are already aware of the [basics of building a HelloWorld](http://developer.android.com/training/index.html) in Android and know [how to use the APIs provided in the support library](http://developer.android.com/training/basics/fragments/support-lib.html)._ _The code example is available on [github](http://github.com/iontech/Fragments_Example "Fragments Example")._ _____________________________________________________________ Ever wanted a code snippet from an Activity to be available to other activities? Perhaps a Button or a ListView, maybe a Layout or any View/ViewGroup for that matter? Fragments let us do just that. Necessity is the mother of invention. Before understanding what Fragments are and how they work, we must first realize their existence in the first place. The Problem ----------- Suppose we have an Android app with two Activities- [*FirstActivity*](https://github.com/iontech/Fragments_Example/blob/master/src/main/java/com/github/iontech/fragments_example/FirstActivity.java) and [*SecondActivity*](https://github.com/iontech/Fragments_Example/blob/master/src/main/java/com/github/iontech/fragments_example/SecondActivity.java). *FirstActivity* contains two Views, a `TextView` (*textView*) and a `Button` (*button1*); and *button1* has an `onClick()` callback that `Toast`'s a simple message "Button pressed". *SecondActivity* contains both the Views present in *FirstActivity* and a `Button` (*button2*). Now we want to utilize the two layout components(Views) of *FirstActivity* in *SecondActivity*, we can go about this with two approaches: 1. Copy and Paste the xml elements of the two Views. 2. Create a separate layout for common Views and reuse it using `` layout element. More about this [here](http://developer.android.com/training/improving-layouts/reusing-layouts.html). Electing the second approach makes sense cause it enables us to make reusable layouts. Everything seems great till now. We are able to make reusable layouts and use them as many times as we want. Now recollect that we have an `onClick()` callback assigned to *button1*. How do we reuse the same callback functionality of *button1* across multiple Activities? `` lets us reuse layouts and not the Activity source. This is where Fragments come into play. Fragments ---------
![image](http://iontech.files.wordpress.com/2013/01/androidfragmentation1-264x300.png)
Fragments encompass both layout resource and Java source. Hence, unlike ``, they allow us to reuse the View components along with their functionality, if needed. Fragments were first introduced in Honeycomb(API 11), living under the `android.app` package. **Note**: API 11 implies that Fragments have no support for devices less than Honeycomb and, for the record, as of writing this post, [more than 50% of Android devices worldwide run versions of Android below Honeycomb](http://developer.android.com/about/dashboards/index.html). Developer dissapointed? You don't have to be, cause google has been cautious enough to add the Fragment APIs to the support library. Yay! In the support library Fragment APIs sit in the `android.support.v4.app` package. This post assumes that your `minSdk` support is below API 11. Hence we concentrate on the Fragment APIs of the support library. ### Diving into code Performing code reuse with Fragments involves three major steps: 1. Creating reusable View components - Creating a layout for the fragment. 2. Creating reusable Java source - Writing the layout's corresponding Fragment class. 3. Employing the reusable components in Activity - Making an Activity to host this Fragment. #### 1. Creating reusable View components ##### Creating a layout for the Fragment This is done precisely as we do it for our activity layouts. The layout contains a root element (ViewGroup) defining the layout, For instance in our example we use a LinearLayout and its child elements(the reusable Views) that we want to have in our fragment. > [fragment_common.xml](https://github.com/iontech/Fragments_Example/blob/master/res/layout/fragment_common.xml)