Fragment Manager in Android

Each Activity includes a Fragment Manager to manage the Fragments it contains. You can access the Fragment Manager using the getFragmentManager method:

FragmentManager fragmentManager = getFragmentManager();

The Fragment Manager provides the methods used to access the Fragments currently added to the Activity, and to perform Fragment Transaction to add, remove, and replace Fragments

Adding Fragments to Activities

Adding Fragments to Activities using XML layouts

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
 android:orientation=”horizontal”
 android:layout_width=”match_parent”
 android:layout_height=”match_parent”>
 <fragment android:name=”com.paad.weatherstation.MyListFragment”
 android:id=”@+id/my_list_fragment”
 android:layout_width=”match_parent” 
 android:layout_height=”match_parent” 
 android:layout_weight=”1”
 />
 <fragment android:name=”com.paad.weatherstation.DetailsFragment”
 android:id=”@+id/details_fragment”
 android:layout_width=”match_parent” 

android:layout_height=”match_parent” 
 android:layout_weight=”3”
 />
</LinearLayout> 

Once the Fragment has been inflated, it becomes a View Group, laying out and managing its UI within the Activity.

This technique works well when you use Fragments to define a set of static layouts based on various screen sizes. If you plan to dynamically modify your layouts by adding, removing, and replacing Fragments at run time, a better approach is to create layouts that use container Views into which Fragments can be placed at runtime, based on the current application state.

Specifying Fragment layouts using container views

<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”
 android:orientation=”horizontal”
 android:layout_width=”match_parent”
 android:layout_height=”match_parent”>
 <FrameLayout
 android:id=”@+id/ui_container”
 android:layout_width=”match_parent” 
 android:layout_height=”match_parent” 
 android:layout_weight=”1”
 />
 <FrameLayout
 android:id=”@+id/details_container”
 android:layout_width=”match_parent” 
 android:layout_height=”match_parent” 
 android:layout_weight=”3”
 />
</LinearLayout> 

Fragment Transactions

Fragment Transactions can be used to add, remove, and replace Fragments within an Activity at run time. Using Fragment Transactions, you can make your layouts dynamic — that is, they will adapt and change based on user interactions and application state.

Each Fragment Transaction can include any combination of supported actions, including adding, removing, or replacing Fragments. They also support the specification of the transition animations to display and whether to include the Transaction on the back stack.

A new Fragment Transaction is created using the beginTransaction method from the Activity’s Fragment Manager. Modify the layout using the add, remove, and replace methods, as required, before setting the animations to display, and setting the appropriate back-stack behavior. When you are ready to execute the change, call commit to add the transaction to the UI queue.

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
// Add, remove, and/or replace Fragments.
// Specify animations.
// Add to back stack if required.
fragmentTransaction.commit();

Adding, Removing, and Replacing Fragments

When adding a new UI Fragment, specify the Fragment instance to add, along with the container View into which the Fragment will be placed. Optionally, you can specify a tag that can later be used to fi nd the Fragment by using the findFragmentByTag method:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.ui_container, new MyListFragment());
fragmentTransaction.commit();

To remove a Fragment, you first need to fi nd a reference to it, usually using either the Fragment Manager’s findFragmentById or findFragmentByTag methods. Then pass the found Fragment instance as a parameter to the remove method of a Fragment Transaction:

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
Fragment fragment = fragmentManager.findFragmentById(R.id.details_fragment);
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();

You can also replace one Fragment with another. Using the replace method, specify the container ID containing the Fragment to be replaced, the Fragment with which to replace it, and (optionally) a tag to identify the newly inserted Fragment.

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.replace(R.id.details_fragment,
 new DetailFragment(selected_index));
fragmentTransaction.commit();

Leave a Comment