Reputation: 1793
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
setupDialog
adding either of dialog.setOnCancelListener();
or dialog.setOnDismissListener();
never gets triggeredonStateChanged
only gets triggered if the user drags the bottomsheet down passed the collapsed state, and there is no state for dismissed/cancelledContactDetailFragment.getDialog().setOnCancelListener()
does not get triggeredGiven that it's essentially a dialog fragment, there must be some way to catch the dismissal?
Upvotes: 6
Views: 12636
Reputation: 1793
Found a simple solution.
Using onDestroy
or onDetach
in the BottomSheetDialogFragment allows me to get the dismissal correctly
Upvotes: 13