When the user clicks an element in a ListView in one fragment, the listener is notified through an interface and it should change the fragment next to the menu.
My current code looks like this
Fragment f = null;
switch(fragmentId) {
case 0:
f = new xFragment();
break;
case 1:
f = new yFragment();
break;
...
}
System.out.println(f.getClass().getName()); // Prints the name of the class correctly
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, f);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
When I select some option from the menu, this function in the activity that contains the fragment is called. It correctly prints the fragmentId and the name of the class is correct as told in the code sample.
However, the fragment isn't changed. I tried to replace the f variable in the replace method by new xFragment() but it didn't help.
The XML layout file looks this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/menuFragment"
android:layout_width="300dp"
android:layout_height="match_parent"
android:name="fi.peltoset.mikko.home.Navigation" />
<LinearLayout
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
The LinearLayout fragmentContainer is the container for the fragments.
At startup I use the same code I showed above except I changed replace to add to add the right fragment when the application starts. This works fine.
What's wrong?