Download Code

Android Fragment LifeCycle Example

In android fragment lifecycle example you will learn about android Fragment and developing a simple android gallery app.So let me give some introduction about Fragments,FragmentManager,FragmentTransaction,LayoutInflater and some other minute modules.

Fragments

A fragment is a particular portion of UI in an activity.One Activity can contain multiple fragments to build flexible UI.Fragments can also reuse in different activities.Fragments can be added at runtime and have their own life cycle just like activity.

Fragments introduced in Android 3.0 (API Level 11).The basic purpose is to design dynamic and flexible UI for different screen sizes.

android fragment,android fragment example,fragment example,android fragment,android fragment lifecycle,android fragment lifecycle example,fragment lifecycle,fragment lifecycle example,

In above picture fragments design to support tablets and Handset depending on screen sizes.Below image is fragments life cycle just like activity life cycle.

android fragment,android fragment example,fragment example,android fragment,android fragment lifecycle,android fragment lifecycle example,fragment lifecycle,fragment lifecycle example,

For more detail you can go to Official Website.

Fragment Manager

FragmentManager used to interact with fragments object inside of an activity.

Fragment Transaction

FragmentTransaction used to perform operations on fragments in your activity like add,remove,replace,transition style.

Layout Inflater

Layout Inflater is used to set layout xml in particular View using inflate() method.

Ok let’s start with building a simple android app.Go ahead and create android fragment lifecycle example application project.

Next create two layouts by right clicking on Layout > New > Android XML File.And put some UI components in that layout.

android fragment,android fragment example,fragment example,android fragment,android fragment lifecycle,android fragment lifecycle example,fragment lifecycle,fragment lifecycle example,

Then create two classes.

android fragment,android fragment example,fragment example,android fragment,android fragment lifecycle,android fragment lifecycle example,fragment lifecycle,fragment lifecycle example,

MainFragment.java

public class MainFragment{
}

CodecFragment.java

public class codecFragment{
}

Now extends both activities with Fragments class.Becuase you want to place that Fragments within the activity.
Next right click in Fragments activity or on Fragments and go to Source > Override Implement/Methods and select onCreateView() method.It has three parameters.

Layout Inflater(to inflate xml layouts)
ViewGroup(view hierarchy of UI components)
Bundle(saved state)
Now use inflater object to access method of LayoutInflater to instantiate xml layout in both activities.
MainFragment.java

package com.example.anonymous.studiofragment;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

 @SuppressLint("NewApi") public class MainFragment extends Fragment{

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		return inflater.inflate(R.layout.main, container, false);
	}

}

codecFragment.java

package com.example.anonymous.studiofragment;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class codecFragment extends Fragment {

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		return inflater.inflate(R.layout.codec, container, false);
	}

}

You are done with Fragments layout.Next open your activity_main.xml.Put fragments inside the parent layout and two button’s.Fragments will have two parameters most important.

  • id(refering id)
  • name(by default Fragments will display on it)

Then your activity_main.xml will look like this.

android fragment,android fragment example,fragment example,android fragment,android fragment lifecycle,android fragment lifecycle example,fragment lifecycle,fragment lifecycle example,

As you can see button parameter onClick.This will call method to change fragments within main XML layout.

Now go to your MainActivity.java class.Create public method with the name selectFragOne() by passing View as a parameter.First declare variable for fragments class.Apply condition on View means which button is pressed and allocate to Fragments variable.Then declare and initialize FragmentManager class object which will communicate with fragments.Last apply all transactions like addition,replacement,removal of fragments and call commit() method.

MainActivity.java

package com.example.anonymous.studiofragment;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void selectFragOne(View view){
        Fragment holder;   // fragment class variable to hold fragment
        if(view == findViewById(R.id.prev)){      // condition to check which button is clicked or view
            holder = new MainFragment();               // allocating to holder variable MainFragment class memory
        }else{
            holder = new codecFragment();          // allocating to holder variable codecFragment class memory
        }

        FragmentManager fm = getFragmentManager();   // object to communicate with fragment
        FragmentTransaction tran = fm.beginTransaction();   // begin fragment add or remove or replace
        tran.replace(R.id.mainFragment, holder);             // replace holder fragment with existing
        tran.addToBackStack(null);                          // back stack setting null which means nothing is at back
        tran.commit();                                    // method to apply settings means replacment of fragment

    }

}


That’s it i hope you guy’s got the idea of Fragment using this android fragment lifecycle example tutorial.Thanks for visiting don’t forget to leave your feedback.
Thanks.