Android Fragment – Android Fragment Example | Fragment Lifecycle

Android Fragment – Android Fragment Example-Fragment Lifecycle

The fragment is a small part of Activity. Android fragment splits the Activity into Sub Activity called Fragment that fits into an Activity.

This feature was first time introduced in Android 3.0 HoneyComb (API level 11).

There is no need to add any permissions inside the manifest file to create the fragment in your application.

It provides us a way to give a consistent UI that is optimized for a wide variety of Android devices, screen sizes etc.

We can also reuse the same fragment in different activities.

A single Activity can contain multiple fragments that’s why we say a Fragment is a type of Sub Activity.

Each fragment has its own lifecycle, which is closely related to the lifecycle of host Activity. So it means when our Activity is stopped then the fragments which are available in activity are also stopped.

Create Fragment class in your application

To create a Fragment in your application we have to extend its subclass with Fragment and override its methods.

public class MyFragment extends Fragment{

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

Android Fragment Lifecycle

The following figure shows the Fragment Lifecycle. Let’s see the methods of Fragment in Android.

Android fragment
Fragment Lifecycle

onAttach()

This method is called when the fragment has been associated with the activity.

onCreate()

This method is called by the system when creating the android fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.

onCreateView()

The system calls this when it’s time for the android fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment’s layout.

onActivityCreated()

It is called when the activity’s onCreate() method has returned, also called when the fragment is being disassociated from the activity.

onStart()

This method is called once the fragment gets visible.

onResume()

The fragment is visible in the running activity.

onPause()

The system calls this method as the first indication that the user is leaving the fragment.

onStop()

The android fragment is not visible. Either the host activity has been stopped or the fragment has been removed from the activity but added to the back stack.

 onDestroyView()

It is called when the view hierarchy associated with the fragment is being removed.

onDestroy()

This method is called to do final cleanup of the fragment’s state but not guaranteed to be called by the Android platform.

onDetach()

It is called when the fragment is being disassociated from the activity.

Perform Fragment Transaction

We can use android fragment in our activity to add, remove, replace, and perform other actions with them in response to user interaction.

Each set of changes that you commit to the activity is called a transaction and you can perform on using APIs in FragmentTransaction.

You can acquire an instance of FragmentTransaction from the FragmentManager like this:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();