BottomSheetDialogFragment - listen to dismissed by user event
<p>How can I listen to a FINAL dismissal of a <code>BottomSheetDialogFragment</code>? I want to save user changes on the final dismissal only...</p>

<p>I tried following:</p>

<p><strong>Method 1</strong></p>

<p>This only fires, if the dialog is dismissed by swiping it down (not on back press or on touch outside)</p>

<pre><code>@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    Dialog d = super.onCreateDialog(savedInstanceState);
    d.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {

            BottomSheetDialog d = (BottomSheetDialog) dialog;   
            FrameLayout bottomSheet = (FrameLayout) dialog.findViewById(android.support.design.R.id.design_bottom_sheet);

            BottomSheetBehavior behaviour = BottomSheetBehavior.from(bottomSheet);
            behaviour.setState(BottomSheetBehavior.STATE_EXPANDED);
            behaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
                @Override
                public void onStateChanged(@NonNull View bottomSheet, int newState) {
                    if (newState == BottomSheetBehavior.STATE_HIDDEN)
                    {
                        // Bottom Sheet was dismissed by user! But this is only fired, if dialog is swiped down! Not if touch outside dismissed the dialog or the back button
                        Toast.makeText(MainApp.get(), "HIDDEN", Toast.LENGTH_SHORT).show();
                        dismiss();
                    }
                }

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

                }
            });
        }
    });
    return d;
}
</code></pre>

<p><strong>Method 2</strong></p>

<p>This does not allow me to distinguish between a final dismissal and one that is coming from a screen rotation or activity recreation...</p>

<pre><code> @Override
public void onDismiss(DialogInterface dialog)
{
    super.onDismiss(dialog);
    // this works fine but fires one time too often for my use case, it fires on screen rotation as well, although this is a temporarily dismiss only
    Toast.makeText(MainApp.get(), "DISMISSED", Toast.LENGTH_SHORT).show();
}
</code></pre>

<p><strong>Question</strong></p>

<p>How can I listen to an event that indicates, that the user has finished the dialog?</p>