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):
- Introduction to Android Fragment?
- How to create Android Fragment?
- 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:
- 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
- A fragment has its own layout and its own behavior with its own life cycle callbacks similar to Activity.
- We can add or remove fragments in an activity at any point of time while the activity is running.
- 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.
- Same fragment can be used in multiple activities.
- 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.
- Fragments were added to the Android API in Honeycomb version of Android whose API version is 11.
Fragment – Design Philosophy
- Fragment was introduced just to support more and flexible UI designs for all types of Screens such as Smartphone, Tablet, Phablet etc.,
- 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.
- By dividing the layout of an Activity into Fragments, it is easy to modify Activity’s appearance at runtime with much ease.
- 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.
- Both the fragments appear in one activity, side by side, and each fragment has its own set of life cycle.
- 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:
- 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
- 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
- Easy to handle the lifecycle and logic of the individual portion of the Activity
- 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.