Android Fragments

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Android Fragments

Android Fragment is the part of activity, it is also known as sub-activity. There can be more than one fragment in an activity. Fragments represent multiple screen inside one activity.

Android fragment lifecycle is affected by activity lifecycle because fragments are included in activity.

Each fragment has its own life cycle methods that is affected by activity life cycle because fragments are embedded in activity.

The FragmentManager class is responsible to make interaction between fragment objects.

Android Fragment Lifecycle

The lifecycle of android fragment is like the activity lifecycle. There are 12 lifecycle methods for fragment.

No.

Method

Description

1)

onAttach(Activity)

it is called only once when it is attached with activity.

2)

onCreate(Bundle)

It is used to initialize the fragment.

3)

onCreateView(LayoutInflater, ViewGroup, Bundle)

creates and returns view hierarchy.

4)

onActivityCreated(Bundle)

It is invoked after the completion of onCreate() method.

5)

onViewStateRestored(Bundle)

It provides information to the fragment that all the saved state of fragment view hierarchy has been restored.

6)

onStart()

makes the fragment visible.

7)

onResume()

makes the fragment interactive.

8)

onPause()

is called when fragment is no longer interactive.

9)

onStop()

is called when fragment is no longer visible.

10)

onDestroyView()

allows the fragment to clean up resources.

11)

onDestroy()

allows the fragment to do final clean up of fragment state.

12)

onDetach()

It is called immediately prior to the fragment no longer being associated with its activity.

Android Fragment Example

Let's have a look at the simple example of android fragment.

activity_main.xml

File: activity_main.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent" >  
  4.   
  5.     <fragment  
  6.         android:id="@+id/fragment2"  
  7.         android:name="com.example.fragmentexample.Fragment2"  
  8.         android:layout_width="0px"  
  9.         android:layout_height="match_parent"   
  10.         android:layout_weight="1"  
  11.         />  
  12.   
  13.     <fragment  
  14.         android:id="@+id/fragment1"  
  15.         android:name="com.example.fragmentexample.Fragment1"  
  16.         android:layout_width="0px"  
  17.         android:layout_height="match_parent"  
  18.         android:layout_weight="1"  
  19.          />  
  20.   
  21. </LinearLayout>  
File: fragment1.xml
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:background="#00ff00"  
  7.      >  
  8.   
  9.     <TextView  
  10.         android:id="@+id/textView1"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="fragment frist"  
  14.         android:textAppearance="?android:attr/textAppearanceLarge" />  
  15.   
  16. </LinearLayout>  

File: fragment2.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  1.     android:layout_width="match_parent"  

  2.     android:layout_height="match_parent"  

  3.     android:orientation="vertical"  

  4.     android:background="#0000ff"  

  5.      >  

  6.   

  7.     

  8.         android:id="@+id/textView1"  

  9.         android:layout_width="wrap_content"  

  10.         android:layout_height="wrap_content"  

  11.         android:text="Second Fragment"  

  12.         android:textAppearance="?android:attr/textAppearanceLarge" />  

  13.   

  14.   


MainActivity class

File: MainActivity.java

  1. package com.example.fragmentexample;  

  2.   

  3. import android.os.Bundle;  

  4. import android.app.Activity;  

  5. import android.view.Menu;  

  6. public class MainActivity extends Activity {  

  7.   

  8.     @Override  

  9.     protected void onCreate(Bundle savedInstanceState) {  

  10.         super.onCreate(savedInstanceState);  

  11.         setContentView(R.layout.activity_main);  

  12.     }  

  13. }  


File: Fragment1.java

  1. package com.example.fragmentexample;  

  2.   

  3. import android.app.Fragment;  

  4. import android.os.Bundle;  

  5. import android.view.LayoutInflater;  

  6. import android.view.View;  

  7. import android.view.ViewGroup;  

  8.   

  9. public class Fragment1 extends Fragment {  

  10.     @Override  

  11.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  

  12.             Bundle savedInstanceState) {  

  13.         // TODO Auto-generated method stub  

  14.         return inflater.inflate(R.layout.fragment1,container, false);  

  15.     }  

  16.   

  17. }  


File: Fragment2.java

  1. package com.example.fragmentexample;  

  2.   

  3. import android.app.Fragment;  

  4. import android.os.Bundle;  

  5. import android.view.LayoutInflater;  

  6. import android.view.View;  

  7. import android.view.ViewGroup;  

  8.   

  9. public class Fragment2 extends Fragment {  

  10.       

  11.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  

  12.             Bundle savedInstanceState) {  

  13.         // TODO Auto-generated method stub  

  14.         return inflater.inflate(R.layout.fragment2,container, false);  

  15.     }  

  16.   

  17. }  

Output

android fragment