Home > front end >  Kotlin Dialog On Dismiss Listener
Kotlin Dialog On Dismiss Listener

Time:08-29

I created a custom dialog and I need to do something back in the fragment that called that dialog, when the dialog is dismissed. I tried a number of things I translated from Java but most didn't work or were deprecated. Any suggestions on how to do this would be appreciated.

DialogFragment:

class MyDialogFragment : DialogFragment() {

onAccept() {
   //do some things
   onDismiss()
}

companion object {

        private const val TAG = "My Dialog Fragment"

        fun show(
           
        ) {
            MyDialogFragment().apply {
                //args
            }.show(fragment.parentFragmentManager, TAG)
        }
    }
}

In the fragment it just called as follows:

class doStuffFragment : AppFragment {

   fun showDialog(){
      MyDialogFragment.show(this)
   }
}

CodePudding user response:

You can override onCancel() method in your corresponding Dialog Fragment. you can get more details by Clicking here.

override fun onCancel(dialog: DialogInterface) {
        super.onCancel(dialog)
        //Your code goes here.
    }

CodePudding user response:

If you are using Fragment to show dialog, you should use childFragmentManager, that will provide new FragmentManager inside Fragment.

class SomeFragment : Fragment() {
    fun showDialogFromFragment() {
        DialogFragment()
            .show(this.childFragmentManager, TAG)
    }
}

Beside that, in case of you are wanna show dialog inside Activity, supportFragmentManager is your choice.

class SomeActivity : AppCompatActivity() {
    fun showDialogFromActivity() {
        DialogFragment()
            .show(this.supportFragmentManager, TAG)
    }
}
  • Related