Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch dismissal of BottomSheetDialogFragment

Is there any way to catch the dismissal/cancel of a BottomSheetDialogFragment?

Bottom sheet class

public class ContactDetailFragment extends BottomSheetDialogFragment
{
    private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback()
    {
        @Override
        public void onStateChanged(@NonNull View bottomSheet, int newState)
        {
            if (newState == BottomSheetBehavior.STATE_HIDDEN)
            {
                dismiss();
            }
        }

        @Override
        public void onSlide(@NonNull View bottomSheet, float slideOffset)
        {
        }
    };

    @Override
    public void setupDialog(Dialog dialog, int style)
    {
        super.setupDialog(dialog, style);
        View contentView = View.inflate(getContext(), R.layout.fragment_contactdetail, null);

        dialog.setContentView(contentView);

        BottomSheetBehavior mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent()));
        if (mBottomSheetBehavior != null)
        {
            mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
            mBottomSheetBehavior.setPeekHeight((int) DisplayUtils.dpToPixels(CONTACT_DETAIL_PEEK_HEIGHT, getResources().getDisplayMetrics()));
        }
    }
}

What I've tried that doesn't work

  • in setupDialog adding either of dialog.setOnCancelListener(); or dialog.setOnDismissListener(); never gets triggered
  • the bottomsheet behavior's onStateChanged only gets triggered if the user drags the bottomsheet down passed the collapsed state, and there is no state for dismissed/cancelled
  • adding the same oncancel/ondismiss listeners to the instantiation of the BottomSheetDialogFragment, by using ContactDetailFragment.getDialog().setOnCancelListener() does not get triggered

Given that it's essentially a dialog fragment, there must be some way to catch the dismissal?

like image 679
behelit Avatar asked Mar 28 '16 05:03

behelit


People also ask

What is BottomSheetDialogFragment?

↳ com.google.android.material.bottomsheet.BottomSheetDialogFragment. Modal bottom sheet. This is a version of DialogFragment that shows a bottom sheet using BottomSheetDialog instead of a floating dialog.

How do I make my bottom sheet not dismissable?

setCancelable(false) will prevent the bottom sheet dismiss on back press also.

How can I dim the background when Bottomsheet is displayed without using dialog?

Using the Interface - onSlide which as a parameter slideOffSet of type float , can be used to dim the background. The new offset of this bottom sheet within [-1,1] range. Offset increases as this bottom sheet is moving upward.

What is persistent bottom sheet?

In android, there are two types of bottom sheets that use most of the time, Persistent Bottom Sheet and Modal Bottom Sheet. Persistent Bottom Sheet: It shows in-app content. It will display at the bottom of the mobile screen making some amount of the content visible.


1 Answers

Found a simple solution. Using onDestroy or onDetach in the BottomSheetDialogFragment allows me to get the dismissal correctly

like image 142
behelit Avatar answered Oct 02 '22 05:10

behelit