Hello Guys today I am going to write tutorial on the Fragment component and class in Android. In this tutorial I will cover the capabilities and usage of fragments in your applications and how they can enable us to create better designed components for applications designed to run on devices ranging from small smartphones to large tablets.
Introduction to fragments
So Let’s look at what fragments really are ,so fragments is a piece of activity which enables more modular activity design .It will be not wrong If we say that fragments is kind of sub Activity.Let’s look more detail in what fragments are,so fragments represent behavior or a portion of user interface in activity.It has its own layout file and life cycle call back methods similar to activity.Fragment life cycles are closely related to its host activity which means when the activity is paused then all the its fragment will also stop.Fragment can be used in multiple activities because the same kind of code can be used in different activities.A single activity can have more than one fragments inside activity.
What are Fragments?
● A Fragment represents a behavior or a portion of user interface in an Activity.
● Has its own layout and life cycle method.
● Add remove dynamically or have multiple fragments
● Created to help design UI with components that “can be-reused in different parts of an application’s user interface
● Introduced in Honeycomb – API version 11
The fragment class was first introduced in Android API level 11 which correlates to Android 3.0.x or the honeycomb release of Android. This was the Android version released in order to enable Android to run on Tablet devices. One of the goals of the fragment class was to help design UI with components that could be reused in different parts of an applications interface. Meaning if we had the same component that would need to take up the left half of the screen on a tablet and the whole screen on a phone then we design that component as a fragment so it can be reused easily for both the tablet and the phone requirements.
Although fragments were released with Android 3.0.x there is an implementation of android.support.v4.app.Fragment that allows for fragment backwards compatibility for designing applications with fragments for Android APIs prior to honeycomb. This backwards compatible class is available through the Android support library.
Fragments are meant to be a complete UI and logical package for the specific component they are implementing, so they have their own UI and life cycles to manage. As described via the android developer blog, “In some ways you can think of a Fragment as a mini-Activity, though it can’t run independently but must be hosted within an actual Activity” from https://android-developers.blogspot.co.in/2011/02/android-30-fragments-api.html
Fragments are embedded within a host activity. These host activities can contain one or more fragments and will ultimately be responsible for managing the fragments through things like life cycle changes and requests for presenting views. Allowing for easier plug and use capabilities, fragments can conveniently be added to layouts for activities through the activity’s layout xml.
Since there is more screen real estate in a tablet than in a phone the design of each screen may be affected in particular the amount of components that can be fit on one screen. In the example seen here we have two fragments, fragment a and fragment b. Each fragment has a desired UI that should be presented to the user. While displaying fragment a along side fragment b in a tablet offers a comfortable user interface for the user, displaying them alongside each other on a phone would be crowded and counter productive. For that reason we are able to design an application that when used on a tablet displays the two UI components for fragment a and fragment b side by side and when run on a phone device displays first fragment a and then after a user interaction opens a new activity with fragment b.
Swipe Views (ViewPager with fragments)
One of the common implementations of fragments in Android applications comes in conjunction with the Viewpager element. A view pager will allow a user to swipe horizontally in order to page through several different views. Fragments offer a useful and effective tool for displaying each screen inside of the viewpager. This allows us to easily separate the logic and the UI for each screen in the viewpager to its own separate fragment.
The basic uses of fragments is:
Fragments are designed to use the device UI space efficiently. When you are writing an application in android, then some people can download it into phone, some into tablets. If you see the space in tablets it will be little bigger than phones. You should be able to use that space efficiently. But you can’t keep writing different applications one targeting for phone, and other targeting for tablets. In order to do it efficiently, i.e writing only application that can fit well with all screen sizes, we use fragments concept.
fragments are designed as a reusable UI components between more than one activity. Once you design a fragment, you can view it as a detachable independent unit, so that you can plug it into any activity where ever there is a space. That means you can reuse the code designed for a fragment.
Fragment you can think of it like a sub activity, which sits with in an activity and which contributes its own UI to the activity screen. Fragments are always part of an activity. With out an activity, a fragment will not exist. So your fragment life cycle will always be affected by activity life cycle. An activity can contain more than one fragment. Similarly a fragment can be re used in multiple activities.
Thank you for reading this introduction to the Fragment component in Android stay tuned for our next tutorial where I further cover more topic related to fragments.