3

So I am working on a project I want to run on legacy Android devices so I am using the compatibility library. I am using an interface similar to NewsReader, only instead of the two fragments being in the Activity, they are embedded in another Fragment, which is embedded in a ViewPagger.

For simplicity we will use these terms...

Activity -> ViewPager -> ContainerFragment->Fragment1
                                          ->Fragment2

In ContainerFragment I am trying to replace out fragment 1 with fragment 2 if it is a phone so I tried the following code in ContainerFragment...

import android.support.v4.app.FragmentTransaction;    
...
public void onBarSelected(Integer index) {
    selectedBarIndex = index;
    if (isDualPane) {
        // display it on the article fragment
        mBarEditFragment.displayBar(index);
    }
    else {
        // use separate activity
        FragmentActivity activity = (FragmentActivity)getActivity();
        FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
        ft.replace(R.id.bar_container, new BarEditFragment(),R.id.bar_edit);
    }

}

But I get the following compile error

Type mismatch: cannot convert from android.app.FragmentTransaction 
 to android.support.v4.app.FragmentTransaction

I double checked and the activity does extend the compatibility FragmentActivity.

UPDATE

Tried to change to...

and got....

MainActivity activity = (MainActivity)getActivity();
Object test = activity.getFragmentManager().beginTransaction();
FragmentTransaction ft = (FragmentTransaction)test;
ft.replace(R.id.bar_container, new BarEditFragment());

And I got...

java.lang.ClassCastException: android.app.BackStackRecord cannot be cast to android.support.v4.app.FragmentTransaction

Any ideas?

ANSWER:

I figured out my problem The issue is you shouldn't get the Fragment manager from the Activity and should get it from the fragment instead.

This works....

public void onBarSelected(Integer index) {
    selectedBarIndex = index;
    if (isDualPane) {
        // display it on the article fragment
        //mBarEditFragment.displayBar(index);
    }
    else {
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.bar_container, new BarEditFragment());
        ft.commit();
    }

}
Jackie
  • 21,969
  • 32
  • 147
  • 289
  • 1
    Have you also checked you are importing the support library in the ContainerFragment? Also, after the ft.replace(), you really should call ft.commit(). – Richard Lewin Feb 11 '12 at 16:53
  • I.e. "import android.support.v4.app.FragmentTransaction" and not "import android.app.FragmentTransaction". – Richard Lewin Feb 11 '12 at 17:06
  • Yes I am ...import android.support.v4.app.FragmentTransaction; – Jackie Feb 11 '12 at 17:20
  • Dianne Hackborn says embedding fragments can lead to issues: http://stackoverflow.com/questions/6221763/android-can-you-nest-fragments. For that reason I would avoid doing so... – Scott Feb 11 '12 at 17:27
  • Ok, that's one place down. Without seeing all your code, the compile error your getting indicates that somewhere you are importing the standard FragmentTransaction rather than the support FragmentTransaction. Your use of embedding a Fragment in another Fragment could well be the reason for this as Scott points out. – Richard Lewin Feb 11 '12 at 17:30
  • Richard, I checked both the Action (Fragment Action) and the Fragment both are importing properly. – Jackie Feb 11 '12 at 19:16
  • What a crappy answer (no offense) – Jackie Feb 11 '12 at 19:17

1 Answers1

12

Facing same problem solved using

getSupportFragmentManager()

Thanks

sandy
  • 3,311
  • 4
  • 36
  • 47