Android replace fragment still displays some of the replaced fragment

I figured out my problem. Marco wasn’t exactly right but he pointed me in the right direction.

The problem was the the container ID I was passing to the replace method was the ID of the fragment I was replacing, not the ID of the fragment container. This seems to explain why some of the original fragment controls were remaining after the replace – that entire fragment wasn’t being replaced.

I changed it to get the fragment container view ID, and that worked! Here’s the code:

transaction.replace(((ViewGroup)(getView().getParent())).getId(), fragment); 

I found the answer for getting the container view ID of a fragment here, Get fragment’s container view id.

Leave a Comment