-1

I am working on Facebook integration for my application. All of the fragments I am currently using in my application, as per client requirement, is android.app.Fragment. Now, the Fragment we created for Facebook is android.support.v4.app.Fragment. If we convert it into android.app.Fragment, the functionality loginButton.setFragment(this); shows error.

My question is, How can I move from android.app.Fragment to android.support.v4.app.Fragment as this piece of code is giving error:

getActivity().getFragmentManager()
    .beginTransaction()
    .replace(R.id.content_frame,new MainFragment())
    .addToBackStack(null)
    .commit();

The error it shows is:

Wrong 2nd argument type. Found: 'com.nicklodean.nicklodean.fragment.MainFragment', required: 'android.app.Fragment'

Please suggest how can I sort out this issue.

Bryan
  • 14,756
  • 10
  • 70
  • 125
Devraj
  • 1,479
  • 1
  • 22
  • 42

4 Answers4

2

First, you need in your Gradle dependencies

compile 'com.android.support:appcompat-v7:XX.Y.Z'

For the respective SDK XX.Y version you have.

(Really only need the v4 library, but v7 shouldn't hurt)


I can't help you with loginButton.setFragment(this) because I do not know what that is doing... But, you of course need to change the class parameter/import of your loginButton class.

Assuming you are using AppCompatActivity or FragmentActivity, you need to use getActivity().getSupportFragmentManager().

Personal opinion: You should be having callbacks to the Activity from the fragments to manage the FragmentManager (as shown here), not directly pulling it from the Fragment classes using getActivity()

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • to use getActivity().getSupportFragmentManager(), I need to change my app.fragment to android.support.v4.app.Fragment, and I cannot do that cause it is out of requirement – Devraj Nov 08 '16 at 20:06
  • 1
    Then your requirements are conflicting with your code. You asked how to use v4 Fragments, and this is how you do it – OneCricketeer Nov 08 '16 at 20:07
0

just add these dependencies

dependencies {
    compile 'com.android.support:support-v4:18.0.0'
    compile "com.android.support:appcompat-v7:18.0.+"
}
Manzoor Samad
  • 889
  • 1
  • 9
  • 16
0

I hope this helps

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    Fragment oldFragment = getSupportFragmentManager().findFragmentByTag(YOUR_FRAGMENT_TAG);
    if (oldFragment == null) {
        ft.add(yourfragment,YOUR_FRAGMENT_TAG);
        ft.commitAllowingStateLoss();
    } else if (!oldFragment.isAdded()) {
        ft.remove(oldFragment);
        ft.add(yourfragment,YOUR_FRAGMENT_TAG);
        ft.commitAllowingStateLoss();
    }
Tefa
  • 328
  • 1
  • 4
  • 15
  • Not helpful cause I have to have "import android.support.v4.app.Fragment" to use "getSupportFragmentManager()". and that I cannot do because I have to use "android.app.Fragment" here. – Devraj Nov 08 '16 at 22:07
  • try to put android.support.v4.app before declare fragment: android.support.v4.app.Fragment = getSupportFragmentManager().findFragmentByTag(YOUR_FRAGMENT_TAG); – Tefa Nov 08 '16 at 22:18
  • also before FragmentTransaction but android.support.v4.app : android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); – Tefa Nov 08 '16 at 22:21
0

You simply must use the android.support.v4.app.FragmentManager with any android.support.v4.app.Fragment, and the android.app.FragmentManager with any android.app.Fragment. There is no way around this.

If you need to use both versions of the Fragment class together (in a single Activity), you will have to manage the fragments within each FragmentManager separately. Take a look at this answer to see how to do so.

Community
  • 1
  • 1
Bryan
  • 14,756
  • 10
  • 70
  • 125