1

i use fragment As follows :

 img_home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ft = fragmentManager.beginTransaction();
            ft.replace(R.id.freamlayout,fragment_home);
            ft.remove(fragment_setting);
            ft.commit();
        }
    });

    img_setting.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ft = fragmentManager.beginTransaction();
            ft.remove(fragment_home);
            ft.replace(R.id.freamlayout,fragment_setting);
            ft.addToBackStack(null);
            ft.commit();
        }
    });

but when transition to fragment 2 , stays fragment 1 how to fix it ??

img1:

img1

img2:

img2

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
Ali
  • 17
  • 1
  • 10
  • Possible duplicate of [Android: fragments overlapping issue](https://stackoverflow.com/questions/18274732/android-fragments-overlapping-issue) – ADM May 22 '18 at 17:06

2 Answers2

0

You only need to call replace() which does both:

Removes the added fragments, and adds the new one you are passing as the second argument.

See: FragmentTransaction replace

Juan
  • 5,525
  • 2
  • 15
  • 26
0

As i can see in documentation for fragmentTransaction.replace()

Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

Check

  • Be sure you are using v4 fragments, because app fragments are depricated in android P.

Most easy approach

If nothing works for you, put background on parent node of both of fragment layout. so the old fragment does not appear behind this.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212