v4 getFragmentManager with Activity - Incompatible types

53,180

Solution 1

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().

Solution 2

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.

Solution 3

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());
Share:
53,180

Related videos on Youtube

David
Author by

David

Interested in learning.

Updated on July 09, 2022

Comments

  • David
    David almost 2 years

    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.
  • David
    David over 9 years
    Sorry, you are correct. It works for FragmentActivities, too.
  • Rod_Algonquin
    Rod_Algonquin over 9 years
    @David just make sure when you use support you must use the support package As I stated above. :))
  • David
    David over 9 years
    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?
  • Raghunandan
    Raghunandan over 9 years
    @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
  • David
    David over 9 years
    Thanks for your help, you and Rod. I've also found this - stackoverflow.com/questions/10477997/… - that may be useful for those interested.
  • Raghunandan
    Raghunandan over 9 years
    @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.
  • Stanley Ko
    Stanley Ko about 2 years
    In 2020, this is no longer correct. Because FragmentActivity will not implement FragmentManager anymore, it will contain FragmentManager.