Fragment Is Not Being Replaced But Put on Top of the Previous One

Fragment is not being replaced but put on top of the previous one

You are doing two things wrong here:

  1. You cannot replace a fragment that is statically placed in an xml layout file. You should create a container (e.g. a FrameLayout) in the layout and then add the fragment programatically using FragmentTransaction.

  2. 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.

Fragment not being replaced, be placed on top of other fragment

When you use add it just straight add the fragment to the view however when you use replace it removes the current fragment then adds the new one which is what you are looking for since it appears your XML is not set to match_parent

Previous fragment is not being replaced when selecting a navigation menu item

Turns out, the problem was in the ShopRecyclerViewAdapter.java file.

In the line

FragmentManager fm = fragment.getActivity().getSupportFragmentManager

fragment.getActivity made the context null. Grabbing context from view instead solved the problem.

I replaced the above line with the following:

FragmentManager fm = ((FragmentActivity) view.getContext()).getSupportFragmentManager()

Here is the reference to the solution:

Replace fragment from recycler adapter

Fragment.replace doesn't remove one fragment, works with multiple others

That's happen because your fragment container is above the listview and by default if you not use background in a ViewGroup(LinearLayout, RelativeLayout,...) then that's gonna be transparent, so, all you need do is put a background in your fragment container:

fragment_wifi_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.WifiFragment"

android:background="#ffffff"

>
...

TIP:

android:clickable="true"

That's avoid concurrence clicks problems

Replacing Fragments isn't working/Am I executing this the proper way?

Instead of <fragment> use <FrameLayout> in layout xml for activity.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container_id"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Then in FragmentActivity in onCreate add initial fragment (in your case SSFFragment):

    FragmentA fragmentA = new FragmentA();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container_id, fragmentA);
transaction.commit();

From inside fragment you can replace fragment in container.

class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Button button = new Button(getActivity());
button.setText("Replace");
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
FragmentB fragmentB = new FragmentB();
transaction.replace(R.id.container_id, fragmentB);
transaction.commit();
}
});
return button;
}
}

Views from old fragment remain in content view after the fragment is replaced

The solution for me was to set the background color in the xml file as suggested in the original question comments by @Yolo.

android:background="@android:color/white"

After I made this change, pieces of this fragment didn't hang around anymore when I switched to new fragments.

FragmentTransaction add fragment after replace old fragment but onDestroyView called

A call to replace() is analogous to calling remove() on A and them calling add() on B hence it is not possible to save A when you replace it it's onDestroy(), onDestroyView() etc will be called.

But instead of calling replace() you can call add() for B add B on top of A without removing A and add B to back stack to navigate to A on back press

Have a good day , Ali



Related Topics



Leave a reply



Submit