Introduction to Android Fragments

Hello readers, It’s been long time posting new tutorial in my Android Tutorial Blog. I was totally occupied with personal work (Marriage and other stuffs) and also recently moved to USA for onsite assignment so I couldn’t engage myself much into blogging.

Going forward, I am planning to publish one or two tutorials every week. Keep watching this space for more interesting Android tutorials and provide your love and support as you gave before.

In this post, we are going to discuss about Android Fragments – A vague and bit tough to understand topic in Android programming world.

I divided Fragment topic into three different posts as given below (Hope this will help you to get to know much about Fragment and its implementation):

  1. Introduction to Android Fragment?
  2. How to create Android Fragment?
  3. What are the types of Fragments?

Let us start with Introduction to Android Fragment.

What is Android Fragment?

As per Official Android Developer Website:

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).

Here is my quick pointers on Fragment:

  1. A Fragment is a piece of code which represents portion of User interface in an Activity or it can just be a Non UI stuff to handle asynchronous tasks at the background
  2. A fragment has its own layout and its own behavior with its own life cycle callbacks similar to Activity.
  3. We can add or remove fragments in an activity at any point of time while the activity is running.
  4. We can combine multiple fragments in a single activity to build a multi-plane UI so each Fragment will take care of separate tasks associated with the current Acitivity.
  5. Same fragment can be used in multiple activities.
  6. Fragment life cycle is directly associated with the life cycle of its host activity that means when the activity is closed, all the fragments available in the activity will also be stopped.
  7. Fragments were added to the Android API in Honeycomb version of Android whose API version is 11.

Fragment – Design Philosophy

  1. Fragment was introduced just to support more and flexible UI designs for all types of Screens such as Smartphone, Tablet, Phablet etc.,
  2. Since Tablet screen is much bigger than Smartphone, there will be more room to combine and interchange UI components. This can be achieved through Fragment.
  3. By dividing the layout of an Activity into Fragments, it is easy to modify Activity’s appearance at runtime with much ease.
  4. For example, a news application can use one fragment to show a list of articles on the left fragment and another fragment to display an article on the right.
    1. Both the fragments appear in one activity, side by side, and each fragment has its own set of life cycle.
    2. Thus, instead of using one activity to select an article and another activity to read the article, the user can select an article and read it all within the same activity, as illustrated in the below:
      Android Fragments Layout
  5. To continue with the news application example—the application can embed two fragments in Activity A, when running on a tablet-sized device. However, on a handset-sized screen, there’s not enough room for both fragments, so Activity A includes only the fragment for the list of articles, and when the user selects an article, it starts Activity B, which includes the second fragment to read the article. Thus, the application supports both tablets and handsets by reusing fragments in different combinations, as illustrated in the above figure.

Advantages of using Fragments

  1. In my opinion, Fragment is more of a UI benefit. It helps us to show off the same UI in different layout in different screens
  2. Easy to handle the lifecycle and logic of the individual portion of the Activity
  3. Fragments can be used across multiple Activities – Re-usability !

Enough theory, we will see how to implement Fragment in Android application in the next post.

Related Articles

Author: Udhay

Share This Post On
468 ad
Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares