getSupportFragmentManager() undefined in adapter when using AppCompatActivity

14,448

Solution 1

you can try this

FragmentTransaction ft = ((AppCompatActivity) mContext).getSupportFragmentManager()
                        .beginTransaction();

Solution 2

In kotlin it will be like this for example. I have tried it and it work perfectly

val dialog = IntervAddFragment()//The fragment that u want to open for example
       val ft = (context as AppCompatActivity).supportFragmentManager.beginTransaction()
        dialog.show(ft, ContentValues.TAG)

Solution 3

Hi this is simple just pass the reference of YourParent Activity Just use the snippet like this below in Your Adapter class to open fragment

Fragment fragment = new YourFragment();
FragmentManager fm = ((MainActivity) mContext).getSupportFragmentManager();
                FragmentTransaction ft = fm.beginTransaction();
                ft.add(R.id.frame_container, fragment);
                ft.commit();

Solution 4

in adapter use

appCompatActivity object instead of context

it will work

Share:
14,448
user2573690
Author by

user2573690

Updated on June 13, 2022

Comments

  • user2573690
    user2573690 almost 2 years

    I have an activity (MainActivity) which extends AppCompatActivity because I am using some of the material design elements in my app.

    I then have an array adapter with a few fields and a button. This adapter has a separate view and is injected into my MainActivity layout.

    When I click the button on the adapter view, I want to open a new fragment which displays a bunch of text, however, I can't seem to do this and I think it is because I am not extending FragmentActivity in my MainActivity? I read on another post that I should be able to extend AppCompatActivity and still be able to reference the fragment manager...here is my code to open the fragment:

    In my custom array adapter, onClick() of a button:

    holder.desc.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          JobDescFragment fragment= new JobDescFragment();
          FragmentTransaction transaction =      getSupportFragmentManager().beginTransaction();
          transaction.replace(R.id.fragment_container, fragment);
          transaction.addToBackStack(null);  
          transaction.commit();
      }
    });
    

    The error I get is that it cannot resolve getSupportFragmentManager(). What am I doing wrong?

    I am importing android.support.v4.app.Fragment and .FragmentManager in my adapter.

    Thanks in advance for the help!

    EDIT:

    <merge
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    
        <com.lorentzos.flingswipe.SwipeFlingAdapterView
            android:id="@+id/frame"
            android:background="@color/laborswipe_lightgray"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:context=".MainActivity"
            android:layout_gravity="top"   />
    
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </merge>
    
  • user2573690
    user2573690 over 7 years
    I tried this but no luck! It still says it cannot resolve getSupportFragmentManager().
  • Akash kv
    Akash kv over 7 years
    use AppCompatActivity appCompatActivity;
  • user2573690
    user2573690 over 7 years
    Where in my adapter do I use that? I can't extend it, so I am trying it like this ((AppCompatActivity)context).getSupportFragmentManager() ?
  • Akash kv
    Akash kv over 7 years
    i will add another answer check it.. use that way
  • user2573690
    user2573690 over 7 years
    How does that code open the fragment? If I do this.appCompatActivity I still can't get the fragment manager. Doing this ((FragmentActivity)mContext).getSupportFragmentManager().beg‌​inTransaction() works to get around the cannot resolve error, but I still dont see the fragment opening.
  • anhtuannd
    anhtuannd over 7 years
    The context within ArrayAdapter may be ApplicationContext, so casting it is very risky.
  • user2573690
    user2573690 over 7 years
    Thanks for the help! I am creating the adapter in an AsyncTask that is part of my main activity, I tried to adapt your code to move what is in the onCreate to the onPostExecute section of my AsyncTask but this does not work, it cannot resolve the OnDetailsRequestListener method which I created in my activity. Any ideas?