Question: I have a layout for landscape mode that shows a ListView on the left and a FrameLayout on the right. When an item is selected from the list another fragment is added to the FrameLayout 

```
MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.myFrameLayout);
FragmentTransaction ft = fragmentManager.beginTransaction();
if (myFragment == null) {
    myFragment = new MyFragment(uri);
    ft.replace(R.id.myFrameLayout, playerFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commitAllowingStateLoss();
}

```

Later on I press delete in the list view and remove the last item in the list, and I try to remove the fragment so that nothing is shown, but it doesn't work, my fragment remains on the screen. The code for removing is:

```
MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.myFrameLayout);
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.remove(myFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
ft.commitAllowingStateLoss();

```

Any ideas why it is not being removed from the View?

Answer: 
are you declaring the fragment in xml? Citing Dianne Hackborn from google:

> 
> Defining fragments in XML
>  is mostly intended for things that are going to stay around. If you are
>  going to add and remove, you should probably consistently do it dynamically.
> 
> 
> 

hope it helps
If you have a `Fragment` that is not removed on replacing the `Fragment` in the `FrameLayout`? you can remove it by commit a separate removal transition with `FragmentTransaction.TRANSIT_FRAGMENT_CLOSE` on it before a call to the regular replace commit.

```
Fragment removeThisFragment;//Keep a reference to the fragment you want to remove
...
 FragmentManager fragmentManager =  getSupportFragmentManager();
 FragmentTransaction transaction =  fragmentManager.beginTransaction();

 // This fragment "sometimes" do not get replaced so we remove it here if it's added.
        if(removeThisFragment!=null&&removeThisFragment.isAdded()) {
            transaction =  fragmentManager.beginTransaction();
            transaction.remove(removeThisFragment);
            transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
            transaction.commitAllowingStateLoss();
        }

        // update the main content by replacing fragments
        transaction =  fragmentManager.beginTransaction();
        transaction.replace(R.id.content_frame, fragment, TAG_MAIN_FRAGMENT).commit();

```

But I suggest to try and figure out first why the Fragment is not replaced in the first place, check if it's doing some heavy work or something keeping it from getting replaced?? but if you need a quick temporary fix try the above code.