0

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?

MikkoP
  • 4,864
  • 16
  • 58
  • 106

4 Answers4

0

First of all, "fragmentContainer" is not a fragment itself. Also, you can't replace static fragments with each other. They must be "dynamically added" fragments. Take a look at this post

To replace dynamically created fragments, take a look at this post.

Community
  • 1
  • 1
MiStr
  • 1,193
  • 10
  • 17
  • Yea, it's not. I know I can't replace fragments that are created in XML layouts. As I said, the fragment is dynamically added to a LinearLayout. Your second link isn't a SO post or neither it tells how to replace dynamically created fragments. – MikkoP Aug 06 '13 at 16:32
  • Sorry; the first link was an SO post, not the second. – MiStr Aug 06 '13 at 17:45
  • Or possibly start by using the add() instead of replace().. (even though replace effectively removes child fragments) – MiStr Aug 06 '13 at 17:51
  • Also, you can check fragment.isInLayout(). If this is true, then the fragment has already been added, and is perhaps not visible for some reason. – MiStr Aug 06 '13 at 17:56
0

Are you attempting to replace the fragment whose ID is 'menuFragment'? If so then you cannot do this as any fragment specified in the XML layout cannot be replaced.

D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
  • No I'm not. I'm trying to replace a dynamically added fragment inside the LinearLayout `fragmentContainer`. – MikkoP Aug 06 '13 at 16:33
0

Can you try to replace the LinearLayout by FrameLayout?

please refer to https://groups.google.com/forum/#!topic/android-developers/gAhaoLlBob4 even though nobody explain why.

Weibo
  • 1,042
  • 8
  • 18
  • It doesn't have any effect. Why would it? – MikkoP Aug 06 '13 at 16:40
  • Looks this guy worked fine after change to FrameLayout http://stackoverflow.com/questions/9019979/android-3-adding-a-fragment-to-a-linearlayout-fill-parent-does-not-work – Weibo Aug 06 '13 at 16:42
0

What version of Android are you targeting?

Try using the support fragment manager:

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html#getSupportFragmentManager()

blackcj
  • 3,651
  • 2
  • 25
  • 23
  • The minimum is 14 and the target is 17. Eclipse gives error if I try to use `getSupportFragmentManager()` method. For which level is it for? – MikkoP Aug 06 '13 at 18:12
  • getSupportFragmentManager is for API level 10 and lower, you shouldn't need it if your minimum is 14. Can you post more of your code? What does your fragment look like? I don't see anything wrong with the snippets you have posted. – blackcj Aug 06 '13 at 18:46