Why isn’t my Fragment showing? A quirk of FragmentTransactions

March 26, 2015

I stumbled upon an interesting situation while working on an Android project. I was trying to display two different Fragments at the same time, but one of them wasn’t showing up and I wasn’t getting any feedback as to why it wasn’t showing up. I was also unable to find any documentation that might explain why it was happening.

In the Android ecosystem, a Fragment is a self-contained portion of the user interface, which may or may not have graphical components. For those Fragments that do have graphical components, they can be inserted into a View in an Activity using a FragmentTransaction. A FragmentTransaction keeps track of one or many add, remove, or replace operations you want to perform in a single transaction.

An interesting quirk comes into play if multiple operations are performed in a single FragmentTransaction. Let’s say you have two fragments, FragmentA and FragmentB. Let’s add FragmentA to fragmentContainerView1, a View in an Activity:

getSupportFragmentManager().beginTransaction()
 .add(R.id.fragmentContainerView1, fragmentA)
 .commit();

Later on, we want to perform a transaction where FragmentA is placed in fragmentContainerView1 and FragmentB is placed in fragmentContainerView2. However, FragmentA may or may not still be in fragmentContainerView1.

Let’s attempt to perform this operation:

getSupportFragmentManager().beginTransaction()
 .replace(R.id.fragmentContainerView1, fragmentA)
 .replace(R.id.fragmentContainerView2, fragmentB)
 .commit();

If FragmentA weren’t currently in fragmentContainerView1, then this operation will proceed as expected and both Fragments will be placed in their respective Views. However, if FragmentA is currently in fragmentContainerView1 then the first replace() operation wouldn’t go through since FragmentA is already there. What will also happen is that the replace() operation for FragmentB will not go through. There isn’t any warning or indication that FragmentB’s replace() operation did not go through other than that it won’t show up in the expected View.

To prevent this from happening, simply check if the fragment has been added yet or not, and if not, then perform the replace, like so:

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

 if (!fragmentA.isAdded()) {
 fragmentTransaction.replace(R.id.fragmentContainerView1, fragmentA);
 }

 if (!fragmentB.isAdded()) {
 fragmentTransaction.replace(R.id.fragmentContainerView2, fragmentB);
 }

 fragmentTransaction.commit();

If you are experiencing a problem similar to this, you might just be encountering a situation where one of the operations in your FragmentTransaction is failing and is causing the other operations to fail as well.

Joseph Kreiser
Joseph Kreiser
Software Developer

Looking for more like this?

Sign up for our monthly newsletter to receive helpful articles, case studies, and stories from our team.

The impact of tech stack choices on software ROI
Android Development iOS

The impact of tech stack choices on software ROI

October 30, 2024

Choosing the right tech stack is key to optimizing software performance, scalability, and maintenance costs. Learn how we work side-by-side with our clients to make sure their tech stack choices line up with their business goals, budget, and long-term vision.

Read more
To RFP or not to RFP?
Business Process

To RFP or not to RFP?

January 19, 2024

Selecting the right digital product partner is not just about ticking boxes in a request for proposal (RFP). It’s more important to engage in a meaningful discovery process to find a collaborator who can turn your vision into a successful digital reality. Here are three things to watch out for as you search for the perfect digital collaborator.

Read more
Build vs. buy: How to decide between custom software, off-the-shelf, or hybrid solutions
Business Development

Build vs. buy: How to decide between custom software, off-the-shelf, or hybrid solutions

October 9, 2024

Deciding whether to build custom software or buy off-the-shelf involves weighing factors like cost, flexibility, and scalability. While off-the-shelf solutions are quick and affordable, custom software offers more control and long-term adaptability. Sometimes, a hybrid approach combining both options can be the most effective for a business’s unique needs.

Read more
View more articles