Switching Fragments With Code
Tags: android, java, tutorial, kotlin
Published: 2022-09-12 11:21:26

`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:

1. Create a `FragmentTransaction` instance in your activity.
2. Specify the changes the transaction should include.
3. Add the transaction to the backstack.
4. 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 fragment 

- The 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:

1. Create a `FragmentTransaction` in your activity.

```
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
```

2. Specify the changes the transaction should include.

```
transaction.add(R.id.fragmentcontainer, newFragment);
```

3. Add the transaction to the backstack.

```
transaction.addToBackstack("transaction_id");
```

4. Commit the transaction.

```
transaction.commit();
```


**_[Previous article: List Fragments](https://dev.to/petemode/list-fragments-2ppg)_**
**_Coming up next: Dynamic Fragments_**