FragmentTransaction
is a class that enables us to add and remove fragments programatically. It works with the backstack just like the one activities use. All we need is a ViewGroup
to hold the fragments.
I'm going to list the steps required to work with FragmentTransaction
instances. All the code snippets I'll write down will base on the assumption that in our layout xml file there is a FrameLayout
with an id of fragmentcontainer that contains or will contain the fragments we will transact with and that the reference to the new fragment is named newFragment.
The steps required are:
- Create a
FragmentTransaction
instance in your activity. - Specify the changes the transaction should include.
- Add the transaction to the backstack.
- Commit the transaction.
Let's look at each of these steps in detail.
Creating a fragment transaction
Fragment transactions are created inside activities. To create a FragmentTransaction
, you call the method beginTransaction()
on the result of calling getSupportFragmentManager()
.
You can create a FragmentTransaction
with the code snippet below:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Specifying Changes in a Fragment Transaction
There are different changes a FragmentTransaction
can contain. Each change is specified by a method. Some of these methods are add()
, replace()
, and remove()
.
add() and replace()
The add()
method adds a new fragment to the backstack. The code snippet below shows how to use the method:
transaction.add(R.id.fragmentcontainer, newFragment);
The replace()
method replaces the existing fragment with a new one. The code snippet below shows how to use the method:
transaction.replace(R.id.fragmentcontainer, newFragment);
Code breakdown
The add()
and replace()
methods take two arguments. They are:
The id of the
ViewGroup
that contains the fragmentThe new fragment that you want to add or replace the old fragment with.
In the code snippets above, transaction is a FragmentTransaction
instance, fragmentcontainer is the id of the ViewGroup in our activity that contains the fragment, and newFragment is a reference to the new fragment that we want to add to the backstack or replace the current one with.
remove()
The remove()
method removes a fragment from the fragment backstack.
transaction.remove(currentFragment);
Code breakdown
The remove method takes one argument - a reference to the fragment we want to remove. The code snippet above will remove the fragment referenced by currentFragment from the backstack.
Adding a FragmentTransaction to the Backstack
You add a FragmentTransaction to the backstack with the method addToBackStack()
.
This method takes one argument - a string that you use to label the transaction in case you need to retrieve the transaction programatically.
transaction.addToBackstack("transaction_id");
In the code above, the string "transaction_id" can be used later to retrieve the transaction. You can also pass null into the addToBackStack()
method if you don't want to pass a string.
Committing a Transaction
You commit a FragmentTransaction by calling the method commit()
on the transaction instance.
transaction.commit();
When you call commit()
, the changes you saved in the FragmentTransaction
are applied to your application.
Summary
To switch fragments programatically, take these steps:
- Create a
FragmentTransaction
in your activity.
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
- Specify the changes the transaction should include.
transaction.add(R.id.fragmentcontainer, newFragment);
- Add the transaction to the backstack.
transaction.addToBackstack("transaction_id");
- Commit the transaction.
transaction.commit();
Previous article: List Fragments
Coming up next: Dynamic Fragments
Top comments (0)