BottomSheetDialogFragment - listen to dismissed by user event

后端 未结 6 1936
野性不改
野性不改 2021-01-01 08:56

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:

6条回答
  •  感情败类
    2021-01-01 09:41

    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.

提交回复
热议问题