Previous fragment visible under the new fragment

Add this to both fragment parent layout

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

The one which you are replacing and one which you will replace.


when need to remove all views from the parent view you need to call removeAllViews() at container in your onCreateView() method of your fragment.

Here is the code:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

container.removeAllViews(); // Inflate the layout for this fragment
 return inflater.inflate(R.layout.fragment_example, container, false); 
}

Instead of R.id.container put id of fragment like this: ((ViewGroup)getView().getParent()).getId(). Actually it is not replacing the fragment but the previous layout i.e FrameLayout. It works for me and i hope it will work in your case also.