0

I want to dismiss my BottomSheet on Click of cardView in BottomSheet

Here is my code of onBindViewHolder

 class Viewholder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private TextView num_name;
        private TextView call_type;
        private TextView call_duration;
        public Viewholder(@NonNull View itemView) {
            super(itemView);
            num_name = itemView.findViewById(R.id.name_number);
            call_type = itemView.findViewById(R.id.call_type_txt);
            call_duration = itemView.findViewById(R.id.call_duration_txt);

            itemView.setOnClickListener(this);
        }

        private void setData(String name_num, String call_typ, String call_dur){
            num_name.setText(name_num);
            call_type.setText(call_typ);
            call_duration.setText(call_dur);
        }

        @Override
        public void onClick(View view) {
            Toast.makeText(view.getContext(),"clicked", Toast.LENGTH_SHORT).show();
            BottomSheetCall bottomSheetCall = new BottomSheetCall();
            bottomSheetCall.dismiss();
        }
    }

I getting this error

java.lang.IllegalStateException: Fragment BottomSheetCall{31fd510 (dbba722a-e3f4-46d4-826c-f31cbc221bd6)} not associated with a fragment manager.
        at androidx.fragment.app.Fragment.requireFragmentManager(Fragment.java:910)
        at androidx.fragment.app.DialogFragment.dismissInternal(DialogFragment.java:245)
        at androidx.fragment.app.DialogFragment.dismiss(DialogFragment.java:202)
        at com.google.android.material.bottomsheet.BottomSheetDialogFragment.dismiss(BottomSheetDialogFragment.java:47)
        at com.bizcure.waessentials.ui.main.CallAdapter$1.onClick(CallAdapter.java:53)

onClick I want to dismiss Bottomsheet and backto Activity.

Thank you in advance =)

1 Answers1

0

what you have done here will not work as you expect it to be :-

BottomSheetCall bottomSheetCall = new BottomSheetCall();
bottomSheetCall.dismiss();

Here you are creating a new bottomSheet Object so using dismiss() on this object will not work. Instead you need a interface which will provide you a callback to your bottomShett Fragment where you have setup your recyclerView. And in that Callback you need to call simply call BottomSheet inbuild method dismiss(). Here is the answer on S.O which shows how you can create that interface which will provide you callback to your Fragment.

WhiteSpidy.
  • 1,107
  • 1
  • 6
  • 28
  • as you said I did, but still getting same error, please check code, I edited question –  Dec 11 '20 at 08:39
  • Again you are doing the same thing. The link in the answer clearly shows how you can get item click callback to your fragment/Activity. You need to call `dismiss()` in your fragment itself. Also no need to do this `BottomSheetCall bottomSheetCall = new BottomSheetCall();` which you have done in your onClickListener. You just need to call `dismiss()` in your fragment itself whenever the `itemClickListener` has been Called. – WhiteSpidy. Dec 11 '20 at 09:18
  • Now I finally did, Thanks –  Dec 11 '20 at 14:17
  • Please upvote the answer. If it dd help you so that others can find it easily. – WhiteSpidy. Dec 11 '20 at 15:12
  • I am new on StackOverflow and I have 5 reputation score, to upvote answer I need 15 reputation score, after getting 15 in future I will do. Thanks a lot –  Dec 11 '20 at 15:31
  • No worries. Glad to help buddy ! – WhiteSpidy. Dec 11 '20 at 15:51