Fragments In Android

Introduction

 
This article explains Fragments in Android.
 
 
Fragments
 
Fragments are more used for user interface benefit. Fragments are used when the user wants to see two different views of two different classes on the same screen. Fragments were added with the Android when Honeycomb was launched. So if you are developing an application only for Android 3.0 (HoneyComb) or higher then Android provides you access to the Fragments class. You can also access the FragmentManager by calling the getFragmnetManager() method in your Activities.
 
However, most applications are being developed for Android 2.1 Eclairs because Android Eclairs cover most of the market. So if you are developing applications for Android 2.1 Eclairs then you need to install the Android compatibility library to use fragments. When you use the Android Compatibility library you need to extend FragmentActivity to your class to use fragments. You can get the FragmentManager by calling the getSupportFragmentManager() method. Otherwise, all the details will be the same as for Honeycomb.
 
Create Fragments
  
 
For creating Fragments your class must have to extend the Fragment class. The Fragment class has methods, just the same as that of an Activity, like onStart(), onPause, onResume() and onCreate(). Usually you should use onCreateVIew(), onCreate() and onStop() methods in your class.
 
oncreate()
 
The system calls this method when the fragment is created.
 
onCreateView()
 
The system calls this method when Android needs the layout for the fragment.
 
onPause()
 
The system calls this method when the user leaves the Fragment. But it does not mean that the Fragment will be destroyed.
 
Most applications apply this method to use fragments in its application. But you can use various methods depending on your needs.
 
Add user Interface
 
Fragments are a part of an activity and it contributes all its layout to the activity. So to get the layout for the Fragments you will use the OnCreateView() method that must return a view. If you are extending ListFragments to your class to use fragments then the OnCreateView will return a ListView. To get the user interface layout for the Fragment your onCreateView() method provides a LayoutInflator object.
  1. public class Second extends Fragment  
  2. {  
  3.     public View onCreView(LayoutInflater i, ViewGroup container, Bundle savedInstanceState)  
  4.     {   
  5.    return i.inflate(R.layout.second, container,false);  
  6.     }  
The container parameter inside the onCreateView() method inserts the FragmentLayout.
 
The bundle is used to the data from one activity to another. The savedInstaceType parameter is of the Bundle type that provides the data about a previous instance of the Fragment if the fragment is being resumed.
 
Add Fragments
 
To add Fragments to the Activity:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.             android:id="@+id/linearlayout01"  
  4.             android:layout_width="fill_parent"  
  5.             android:layout_height="fill_parent"  
  6.             android:background="#ccc"  
  7.             android:layout_weight="1"  
  8.             android:orientation="vertical">  
  9.         <fragment  
  10.                   android:name="com.fragmentsExample.First"                   
  11.                   android:id="@+id/frag_1"  
  12.                   android:layout_width="fill_parent"  
  13.                   android:layout_height="fill_parent" />  
  14.     </LinearLayout> 
Here the android:name specifies which class should be instantiated in the layout.
 
You get Fragments in your Activity as in the following:
  1. import android.os.Bundle;  
  2. import android.app.Activity;  
  3. import android.support.v4.app.FragmentActivity;  
  4. import android.view.Menu;  
  5.    
  6. public class MainActivity extends FragmentActivity {  
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.     }  
  12.   
  13.     @Override  
  14.     public boolean onCreateOptionsMenu(Menu menu) {  
  15.         // Inflate the menu; this adds items to the action bar if it is present.  
  16.         getMenuInflater().inflate(R.menu.main, menu);  
  17.         return true;  
  18.     }   
Difference between Activity and Fragment
 
One of the major differences between an Activity and a Fragment is that the Activity instantiates its layout using the setContentView() method whereas the Fragment instantiates the views inside the onCreateView() method.
 
Fragments cannot be launched as a component inside your application because it is not a subclass of context and therefore is always live inside the Activity.


Similar Articles