Using Fragments in Android with FragmentActivity

I recently published an Android application that lets you apply a tan to your photos. Rather than creating an Activity for each of the four screens, I used a FragmentActivity with four Fragments. I hope you find this android fragment activity example useful.

Creating the Fragments

Firstly, you need to define your screens by creating classes that extend Fragment. Each of your fragments should override onCreateView() which is called to create the view of your fragment.

ExampleFragment.java

This is an example implementation of one fragment. In a real life example, you will have multiple fragments (one for each screen that you want to have).

package com.example.fragment_activity_example;

import com.example.fragment_activity_example.R;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ExampleFragment extends Fragment {

	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {

		// Create the view from XML layout.
		final View view = inflater.inflate(R.layout.fragment_example, null);

		// Perform additional configuration on layout components here.

		return view;
	}

}

fragment_example.xml

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

Setting up the FragmentActivity

Your activity will extend FragmentActivity. You will need to define a FragmentPagerAdapter and a ViewPager in this class.

MainActivity.java


package com.example.fragment_activity_example;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {

	/**
	 * Identifier for the example fragment.
	 */
	public static final int FRAGMENT_EXAMPLE = 1;

	/**
	 * The adapter definition of the fragments.
	 */
	private FragmentPagerAdapter _fragmentPagerAdapter;

	/**
	 * The ViewPager that hosts the section contents.
	 */
	private ViewPager _viewPager;

	/**
	 * List of fragments.
	 */
	private List<Fragment> _fragments = new ArrayList<Fragment>();

	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.activity_main);

		// Add each fragment to our list.
		this._fragments.add(FRAGMENT_EXAMPLE, new ExampleFragment());

		// Setup the fragments, defining the number of fragments, the screens and titles.
		this._fragmentPagerAdapter = new FragmentPagerAdapter(this.getSupportFragmentManager()){
			@Override
			public int getCount() {
				return MainActivity.this._fragments.size();
			}
			@Override
			public Fragment getItem(final int position) {
				return MainActivity.this._fragments.get(position);
			}
			@Override
			public CharSequence getPageTitle(final int position) {
				// Define titles for each fragment.
				switch (position) {
					case FRAGMENT_EXAMPLE:
						return "Example title";
					default:
						return null;
				}
			}
		};

		this._viewPager = (ViewPager) this.findViewById(R.id.pager);
		this._viewPager.setAdapter(this._fragmentPagerAdapter);

		// Set the default fragment.
		this.openFragment(FRAGMENT_EXAMPLE);
	}

	/**
	 * Open the specified fragment.
	 * @param fragment
	 */
	public void openFragment(final int fragment) {
		this._viewPager.setCurrentItem(fragment);
	}

	/**
	 * Get the fragment object for the specified fragment.
	 * @param fragment
	 * @return
	 */
	public Fragment getFragment(final int fragment) {
		return this._fragments.get(fragment);
	}

}

The additional methods defined, openFragment() and getFragment() are helper methods that I found useful for manipulation of the fragments, especially when trying to go to the next fragment when inside of a fragment.

activity_main.xml

<ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</ViewPager>

That’s all there is to it. Your application can now have multiple screens with only one Activity.

One thought on “Using Fragments in Android with FragmentActivity

  1. riya Reply

    I am new to this. I tried hard to integrate the fragment activities but fail. After lot of waste of time i am still in dark. i have one main activity and 4 separate activities. Can you help me out. If required i can send the code in zip format
    Regards
    Riya

Leave a Reply

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