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 Activites.
However most applications are being developed for Android 2.1 Eclairs because Android Eclairs covers most of the market. So if you are developing applications for Android 2.1 Eclairs then you need to install the Android compatibitlity 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 need.

Add user Interface

Fragments are a part of an activity and it contributes all its layout to the actvity. 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.

public
class Second extends Fragment
{
   
public View onCreView(LayoutInflater i, ViewGroup container, Bundle savedInstanceState)
    { 
  
return i.inflate(R.layout.second, container,false);
    }
}

The container parameter inside the onCreateView() method inserts the FragmentLayout.

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:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

            android:id="@+id/linearlayout01"

            android:layout_width="fill_parent"

            android:layout_height="fill_parent"

            android:background="#ccc"

            android:layout_weight="1"

            android:orientation="vertical">

        <fragment

                  android:name="com.fragmentsExample.First"                 

                  android:id="@+id/frag_1"

                  android:layout_width="fill_parent"

                  android:layout_height="fill_parent" />

    </LinearLayout>
 
Here the android:name specifies which class should be instanciated in the layout.

You get Fragments in your Activity as in the following:

import android.os.Bundle;

import android.app.Activity;

import android.support.v4.app.FragmentActivity;

import android.view.Menu;

 

public class MainActivity extends FragmentActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    } 

}

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.

Up Next
    Ebook Download
    View all
    Learn
    View all