behelit
behelit

Reputation: 1793

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

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

Upvotes: 6

Views: 12636

Answers (1)

behelit
behelit

Reputation: 1793

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

Upvotes: 13

Related Questions