26

I have a simple activity which runs as expected.

import android.app.Activity;
import android.app.FragmentManager;
// import android.support.v4.app.FragmentManager;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // FragmentManager fm = getSupportFragmentManager(); // ActionBarActivity
        FragmentManager fm = getFragmentManager(); // Activity
    }
}

I then replaced

import android.app.FragmentManager;

with

import android.support.v4.app.FragmentManager;

so I could support my older devices.. However, this reports an error:

Incompatible types.

    Required: android.support.v4.app.FragmentManager

    Found: android.app.FragmentManager

What am I doing wrong here?

The popular solution I found is to use getSupportFragmentManager() instead, but this only works for ActionBarActivites [edit - see answers] and FragmentActivities.

cannot convert from android.app.FragmentManager to android.support.v4.app.FragmentManager

The other relevant solution points to using a FragmentActivity instead, but this appears to have the same legacy problems.

The method getFragmentManager() is undefined for the type MyActivity

import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

    public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        // FragmentManager fm = getSupportFragmentManager(); // ActionBarActivity
        FragmentManager fm = getFragmentManager();
    }
}

I'm pretty sure the solution to this will be on SE already, but it isn't easy (for me) to find. The minimal example should help other people with understanding it, too.

  • I'm fairly new to Android.

4 Answers 4

55

In your first case if you use getSupportFragmentManager() you need to extend FragmentActivity or extend ActionBarActivity (extends FragmentActivity) as FragmentActivity is the base class for support based fragments.

In your second case you need to use getSupportFragmentManager() instead of getFragmentManager().

Fragments were introduced in honeycomb. To support fragments below honeycomb you need to use fragments from the support library in which case you need to extend FragmentActivity and use getSupportFragmentManager().

4
  • This isn't the original question as a solution has now been found, but why can I not use a standard activity? Surely the v4 support should allow the activity to function in the same way as the new APIs. Secondly, now I am having to use a FragmentActivity instead of an activity, are there likely to be any other knock-on effects with my code?
    – David
    Commented Aug 15, 2014 at 4:39
  • 1
    @David use FragmentActivity if using v4. Nothing to worry use all imports from support library. I answered both the cases if you read the post again. The incomaptible types is becasue you need to use imports from support library Commented Aug 15, 2014 at 4:40
  • Thanks for your help, you and Rod. I've also found this - stackoverflow.com/questions/10477997/… - that may be useful for those interested.
    – David
    Commented Aug 15, 2014 at 4:42
  • 1
    @David read developer.android.com/reference/android/support/v7/app/…. See this ↳ android.app.Activity ↳ android.support.v4.app.FragmentActivity ↳ android.support.v7.app.ActionBarActivity. Commented Aug 15, 2014 at 4:45
4
use getSupportFragmentManager() instead, but this only works for ActionBarActivites.

Wrong, it should work in your FragmentActivity, that FragmentActivity is in your support package, when you are supporting older device then all your import from activity,fragment, fragment managers, etc. must have android.support.v4. to specify that you are using the support package. without using so will result to incompatible compile time error.

What am I doing wrong here?

You are combining support package with non support package which result to incompatible compile time error, as I said above.

2
  • Sorry, you are correct. It works for FragmentActivities, too.
    – David
    Commented Aug 15, 2014 at 4:36
  • 1
    @David just make sure when you use support you must use the support package As I stated above. :)) Commented Aug 15, 2014 at 4:38
2

for Kotlin guyz u dont have to change to not v4

use below to get the activity as long as its the parent

(activity as MainDrawerActivity)

or

((YourActivityName)getActivity());
0

For use in Java Dialog:

 - import android.app.FragmentManager;
 - FragmentManager fragmentManager;

For Kotlin :

 - (activity as? YourActivity)?.fragmentManager
1
  • In 2020, this is no longer correct. Because FragmentActivity will not implement FragmentManager anymore, it will contain FragmentManager.
    – Stanley Ko
    Commented May 2, 2022 at 10:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.