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?
I have tried above all the case and try to fix the issue but still not got solution. I don't know why its not working for me. I agree with above answers because so many agree with it.
I am just extending the answer and below is the way which is working for me.
I hope someone will get help.
Thank you to Sapan Diwakar and others. This answer helped me. I simply created a RelativeLayout without a fragment inside it, used the RelativeLayout as a parent or group view, specified that group view in the transaction to replace, and it worked.
Code: getSupportFragmentManager().beginTransaction() .replace(R.id.content_group_view, itemDetailFragment).commit();
Layout:
You can use setTransition method from FragmentTransaction to fix overlapping issue.
i had same problem after using this problem solved
Take frame layout where u are replacing ur fragment
and code for replacement
if you want whole example i will send you just comment me......
In Oncreate ()
now this will be always your default fragment ...if you add fragment in xml layout it will always overlap , so set initial content with fragment at run time
Add
android:background="?android:windowBackground">
to the container in which your fragment resides. In in my case I added aFrameLayout
, that should help.