title: Launching BottomSheet from a RecyclerViewAdapter that extends with RecyclerView.Adapter inside onBindViewHolder
body: <p>I have a Card View in a layout that has a two Text Views and an Image View and i have a Recycler View that uses the Card View to set items. What i want to achieve is to launch a Bottom Sheet when the Card View is tapped</p>
  
  <p>Here is the code in Main Activity</p>
  
  <p>Code for List</p>
  
  <pre><code>List&lt;Item&gt; lstItem;
  
      lstItem = new ArrayList&lt;&gt;();
  
      lstItem.add(new Item("Samsung","Smartphones","QualitlstItem = new ArrayList&lt;&gt;();y Samsung Smartphones",R.drawable.samsung,"21000"));
      lstItem.add(new Item("Lenovo","Smartphones","Quality Lenovo smartphones",R.drawable.lenovo,"15000"));
  </code></pre>
  
  <p>Code for attaching Recycler View Adapter to Main Activity</p>
  
  <pre><code>        FragmentManager fragmentManager = getSupportFragmentManager();
          RecyclerViewAdapter2 mainAdapter2 = new RecyclerViewAdapter2(lstItem,fragmentManager);
          mainRecyclerView.setLayoutManager(new GridLayoutManager(this,3));
          mainRecyclerView.setAdapter(mainAdapter2);
  </code></pre>
  
  <p>Recycler View Adapter 2 Code</p>
  
  <pre><code>        import android.content.Context;
          import android.view.LayoutInflater;
          import android.view.View;
          import android.view.ViewGroup;
          import android.widget.ImageView;
          import android.widget.TextView;
  
  
          import androidx.cardview.widget.CardView;
          import androidx.fragment.app.FragmentManager;
          import androidx.recyclerview.widget.RecyclerView;
  
  
          import java.util.List;
  
          public class RecyclerViewAdapter2 extends RecyclerView.Adapter&lt;RecyclerViewAdapter2.MyViewHolder&gt; {
  
          private Context context;
          private List&lt;Item&gt; data;
          private FragmentManager fragmentManager;
  
          public RecyclerViewAdapter2(List&lt;Item&gt; data, FragmentManager fragmentManager) {
          this.data = data;
          this.fragmentManager = fragmentManager;
      }
  
  
          @Override
          public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview_items, parent, false);
            return new MyViewHolder(view);
          }
  
          @Override
          public void onBindViewHolder(final MyViewHolder holder, final int position) {
          holder.tv_item_title.setText(data.get(position).getTitle());
          holder.price.setText(data.get(position).getPrice());
          holder.item_thumbnail.setImageResource(data.get(position).getThumbnail());
  
          holder.cardView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
                  bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
  
              }
          });
      }
  
          @Override
          public int getItemCount() {
          return data.size();
          }
  
          public static class MyViewHolder extends RecyclerView.ViewHolder{
  
          TextView tv_item_title;
          TextView price;
          ImageView item_thumbnail;
          CardView cardView;
  
  
          public MyViewHolder(View itemView){
              super(itemView);
  
              tv_item_title = itemView.findViewById(R.id.itemTitleId);
              price = itemView.findViewById(R.id.priceId);
              item_thumbnail = itemView.findViewById(R.id.itemThumbnailId);
              cardView = itemView.findViewById(R.id.cardView);
          }
  
      }
  
  }
  </code></pre>
  
  <p>This is where am having a problem: Launching Bottom Sheet on Card View Click:</p>
  
  <pre><code>        BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
          bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());
  </code></pre>
  
  <p>BottomSheetFragment Code;</p>
  
  <pre><code>        import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  
  import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
  
  public class BottomSheetFragment extends BottomSheetDialogFragment {
  
  
  
      public BottomSheetFragment() {
  
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          return inflater.inflate(R.layout.bottomsheet1, container, false);
      }
  
      @Override
      public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
  
          //String s=getArguments().getString("myString");
      }
  }
  </code></pre>
  
  <p>I have tried the following inside onBindViewHolder:</p>
  
  <pre><code>        @Override
      public void onBindViewHolder(final MyViewHolder holder, final int position) {
          holder.tv_item_title.setText(data.get(position).getTitle());
          holder.price.setText(data.get(position).getPrice());
          holder.item_thumbnail.setImageResource(data.get(position).getThumbnail());
  
          holder.cardView.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  AppCompatActivity appCompatActivity = new AppCompatActivity();
                  FragmentManager fragmentManager = appCompatActivity.getSupportFragmentManager();
                  BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
                  bottomSheetFragment.show(fragmentManager, bottomSheetFragment.getTag());
  
              }
          });
      }
  </code></pre>
  
  <p>Here is the Error:</p>
  
  <pre><code>        E/AndroidRuntime: FATAL EXCEPTION: main
      Process: manu.apps.victortestapp, PID: 16271
      java.lang.IllegalStateException: Activity has been destroyed
          at androidx.fragment.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:2114)
          at androidx.fragment.app.BackStackRecord.commitInternal(BackStackRecord.java:683)
          at androidx.fragment.app.BackStackRecord.commit(BackStackRecord.java:637)
          at androidx.fragment.app.DialogFragment.show(DialogFragment.java:144)
          at manu.apps.victortestapp.RecyclerViewAdapter2$1.onClick(RecyclerViewAdapter2.java:52)
          at android.view.View.performClick(View.java:6726)
          at android.view.View$PerformClick.run(View.java:25434)
          at android.os.Handler.handleCallback(Handler.java:790)
          at android.os.Handler.dispatchMessage(Handler.java:99)
          at android.os.Looper.loop(Looper.java:164)
          at android.app.ActivityThread.main(ActivityThread.java:6524)
          at java.lang.reflect.Method.invoke(Native Method)
          at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:451)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
  </code></pre>
  
  <p>I want to open BottomSheet when the Card View inside the Recycler View is tapped</p>