Our blog

Using Fragments to Design Android Based Applications

Fragment is a modular approach to design the User interface for the android based application. Fragment concept is introduced in android 3.0 (API level 11). Fragment gives the liberty to user for designing layouts which supports mobile as well as tablet version of the application.

Through this blog I would like to share some of the fundamentals and working of the fragment and how can we achieve the scalable interface with fragment.

Fragment gives immense opportunity to User interface designer to get the fill of mobile and tablet. Fragment runs with the activity which started it and it has its own life cycle as activity has the only difference with life cycle of fragment is it is managed by the activity which hosted the fragment. In the case of activity the life cycle is managed by android system. You can consider the fragment as sub-activity of the activity which runs in its own world managed by host activity. Fragment’s life cycle is affected by the life cycle of the host activity. As the activity pause the fragment pauses and if activity destroys the fragment destroys.

Fragment lives in the ViewGroup when you add them in the activity and stays there till activity lives.

Above image shows the behavior of the fragment on tablet and mobile device. the UI is automatically get adjusted according to device.


Creating Fragment

  1. XML : <fragment> tag is use to add the fragment in the xml file of the android application
  2. By code:   FragmentManager api is used to add the fragment into your application. Which is followed by add, update and delete of the fragment.

Type of Fragment

  1. DiglogFragment: Used to show floating dialog in the activity. Why this is difference from conventional dialog api is that it maintains the stack of the dialog show so application can have stack of the displayed dialog and user can get back the previously shown dialog.
  1. ListFragment:   Displays list of items which are managed by the adapter and it provides several methods to mange that li
  1. PreferenceFragment: Displays list of nested preference objects as similar to preference Activity. This is useful when creating the settings activity for the application.

Fragment Transaction

  1. Add() : adds a fragment to the back stack and shows to the user
  2. Remove() : removes particular fragment from the back stack and manages other fragment accordingly
  3. Replace (): replace the existing fragment with new one.
  4. Commit() : the important call to perform all the above transaction

When to use fragment?

  1. When your application need to support portrait as well as landscape mode
  2. Applications designs have multi pan content
  3. Application needs to support mobile as well as tablets
  4. Re-usability of the UI components

Life Cycle comparison with Activity (Important)

Above image clearly state the fragment’s life cycle method in line with Activities methods so developers needs to keep this things handy while developing for the fragments.

For more details: http://developer.android.com/guide/components/fragments.html

Interested in our Testing Services?

Contact Apexon +1 408-727-1100

By submitting this form, you agree that you have read and understand Apexon’s Terms and Conditions. You can opt-out of communications at any time. We respect your privacy.

By submitting this form, you agree that you have read and understand Apexon’s Terms and Conditions. You can opt-out of communications at any time. We respect your privacy.

Other stories you may enjoy...

One Year In: Technology Success Stories from the Pandemic

This time last year, US companies were forced to face a new and unsettling reality: business as usual was no longer an option. I wrote then about how businesses could shift their...

Healthcare Apps and the Need for Security

One of the most exciting areas in tech right now promises to be “the most personal” ever. A key aspect of making wearable devices like the Apple Watch personal is through...

Developing an App for the 2020 General Election?

Here is a thought: With the UK General Election having just finished, could the next one in 2020 be the first to use a mobile app to allow people to vote? The polling...