Activity:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
Fragment1 fragment = new Fragment1();
Fragment2 fragment2 = new Fragment2();
transaction.replace(R.id.Fragment1, fragment);
transaction.addToBackStack(null);
transaction.commit();
FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();
transaction2.replace(R.id.Fragment1, fragment2);
transaction2.addToBackStack(null);
transaction2.commit();
Code in the view:
<fragment
android:id="@+id/Fragment1"
android:name="com.landa.fragment.Fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="@+id/include1" />
The problem is, the content doesn't really get replaced - it gets put on top (so it overlaps).
When I click back, the first fragment gets shown properly (without the second), but initially both are visible (I want only the last one to be visible).
What am I missing here?
Use a container instead of using fragment
Now in MainActivity
Please note that your fragment class should import 'android.app.Fragment' Make sure, that you use "support.v4" or the "original" implementations and do not mix them. I was getting this problem because I had mixed them.
You are doing two things wrong here:
You cannot replace a fragment that is statically placed in an
xml
layout file. You should create a container (e.g. aFrameLayout
) in the layout and then add the fragment programatically usingFragmentTransaction
.FragmentTransaction.replace
expects the id of the container that contains the fragment and not the id of the fragment as the first parameter. So you should pass the first argument as the id of the container that you added the first fragment to.You can refer to this link for more details.
I also had the same issue, but it was because I had not changed the background color of the fragment that I was trying add to my framelayout.
Try doing this, with
layout_height
andlayout_width
set tomatch_parent
I had a similar problem but my issue was that I was using two different Fragment managers: One from
getSupportFragmentManager()
and one fromgetFragmentManager()
. If I added one fragment with theSupportFragmentManager
and then tried replacing the fragment with theFragmentManager
, the fragment would just get added on top. I needed to change the code so that the sameFragmentManager
would be used and that took care of the issue.I had the same problem and saw all the answers, but none of them contained my mistake! Before trying to replace the current fragment, I was showing the default fragment in the xml of the container activity like this:
after that, although i was passing the
FrameLayout
to thefragmentTransaction.replace()
but i had the exact problem. it was showing the second fragment on top of the previous one.The problem fixed by removing the fragment from the xml and showing it programmatically in the
onCreate()
method of the container activity for the default preview on the start of the program like this:and the container activity xml:
You can clear backStack before replacing fragments: