I think you may have misunderstood my comment, so I will provide a more detailed explanation here.
One problem that usually occurs when deleting or replacing fragments is trying to remove a fragment that was added to the layout through XML, and not programmatically in Java. This is not the same as inflating your own fragment layout in the onCreateView() function of the Java fragment code (this is what you seem to describe in your answer to my comment). To illustrate what I'm talking about, I will show you two ways that people try to remove / replace fragments.
This is the wrong way to do this:
XML layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.example.ExampleFragment" android:id="@+id/example_fragment" android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent" /> </LinearLayout>
Java:
swapFragment() { Fragment newFragment = new ExampleFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); transaction.commit(); }
This code will not execute as you expect. The source fragment added to the XML layout will not be deleted. This is because XML layouts are intended to describe the elements of a static layout. You can change their contents at runtime or hide them, but you cannot remove these things from the layout. This is what Diana Hackborn talked about in the discussion topic that I’ve been involved with before.
This is the right way to do this (at least in my experience):
XML layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout>
Java:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.my_layout); ExampleFragment newFragment = new ExampleFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.add(R.id.fragment_container, newFragment); transaction.commit(); }
...
swapFragment() { Fragment newFragment = new ExampleFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); transaction.commit(); }
This strategy does not add a fragment to the original layout. Instead, it adds it to the Java code when creating the Activity. This allows you to remove it from the layout (using remove() or replace() )
This may not solve your problem, but it is a common difficulty created by the Fragments. You can make sure that you add Fragments appropriately so that they can be deleted, and then, if this does not solve the problem, we can fix the problems.