How can I listen to a FINAL dismissal of a BottomSheetDialogFragment
? I want to save user changes on the final dismissal only...
I tried following:
In an AppCompatActivity
you can use the following technique:
val mgr = supportFragmentManager
val callback = object: FragmentManager.OnBackStackChangedListener {
var count = 0
override fun onBackStackChanged() {
// We come here twice, once when the sheet is opened,
// once when it's closed.
if (++count >= 2) {
mgr.removeOnBackStackChangedListener(this)
doWhatNeedsToBeDoneWhenTheSheetIsClosed()
}
}
}
mgr.addOnBackStackChangedListener(callback)
Be sure to do the above just before you call show
on the sheet.