Android Fragments: Building dynamic UI, Why to use Fragments?

Fragment is a UI entity attached to Activity. Fragments can be reused by attaching in different activities. Activity can have multiple fragments attached to it. Fragment must be attached to an activity and its lifecycle will depend on its host activity. Multiple fragments attached to host activity can be operated separately such as add or remove or replace them.  Fragments operations are managed by FragmentManager and each operation or task on activity is performed with FragmentTransanction. With Fragments you can perform better optimization for your application in order to adapt UI for phone as well as tablets.

[su_button url=”” target=”blank” style=”stroked” background=”#51d461" color=”#ffffff” size=”6" center=”yes” radius=”0" icon=”icon: arrow-circle-o-down”]Download Complete Source Code[/su_b[/su_button]p> Lifecycle of a fragment

  1. onAttach(Activity) called once the fragment is associated with its activity.
  2. onCreate(Bundle) called to do initial creation of the fragment.
  3. onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment.
  4. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().
  5. onViewStateRestored(Bundle) tells the fragment that all of the saved state of its view hierarchy has been restored.
  6. onStart() makes the fragment visible to the user (based on its containing activity being started).
  7. onResume() makes the fragment interacting with the user (based on its containing activity being resumed).

 As a fragment is no longer being used, it goes through a reverse series of callbacks:

  1. onPause() fragment is no longer interacting with the user either because its activity is being paused or a fragment operation is modifying it in the activity.
  2. onStop() fragment is no longer visible to the user either because its activity is being stopped or a fragment operation is modifying it in the activity.
  3. onDestroyView() allows the fragment to clean up resources associated with its View.
  4. onDestroy() called to do final cleanup of the fragment’s state.
  5. onDetach() called immediately prior to the fragment no longer being associated with its activity.

Sample Fragment Application

Create a android project in Eclipse, Name your application an set package. I have used name SampleFragmentApplication and package name as com.androidsrc.fragment.

We will attach two fragments to main activity of application. One fragment will be of type ListFragment. ListFragment gives you combo of ListView and Fragment functionality which is very easy to use. In ListFragment, we will set adapter for colors in ListView. On clicking of item of this listview, we will interact with second fragment and set corresponding color to its ImageView.

Lets start with layout files. Modify your res/layout/actvity_main.xml as below

You may also like...

1 Response

  1. February 24, 2015

    […] for views to be shown as pages. We will use FragmentPagerAdapter for our ViewPager which will have Fragment as its data item. As soon as Page of ViewPager is changed, we will update page […]

Leave a Reply

Your email address will not be published. Required fields are marked *