1

I have this dialog fragment class:

class AskDownloadFragment : DialogFragment() {
private lateinit var navController: NavController
private lateinit var mainActivity: MainActivity

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

    mainActivity = activity as MainActivity

    return activity.let {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder(it).setMessage(R.string.want_to_download)
            .setNegativeButton(R.string.no) { _, _ ->
                dismiss()
            }

            .setPositiveButton(R.string.yes) { _, _ ->
                dismiss()
                mainActivity.showDownloadDialog()
            }
            .setCancelable(false)

            // Create the AlertDialog object and return it
            .create()
    }
}

}
And I cal it this way:

AskDownloadFragment().show(supportFragmentManager, DialogTags.TAG_ASK_DOWNLOAD.toString())

I can see the dialog on the screen, but I'm still able to close it touching outside alertDialog. What can I do?

Max Siomin
  • 95
  • 1
  • 7
  • If it helps, AlertDialog has setCanceledOnTouchOutside(boolean); function, according to the docs it's a function from the Dialog class that you are deriving from, check if it works (don't know how to write it in kotlin though) – Dan Baruch Jun 17 '21 at 07:27

1 Answers1

2

Just simply use this key

.setCanceledOnTouchOutside(false)

So overall it will be like

class AskDownloadFragment : DialogFragment() {
private lateinit var navController: NavController
private lateinit var mainActivity: MainActivity

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

    mainActivity = activity as MainActivity

    return activity.let {
            // Use the Builder class for convenient dialog construction
            val dialog = AlertDialog.Builder(it).setMessage(R.string.want_to_download)
                    .setNegativeButton(R.string.no) { _, _ ->
                        dismiss()
                    }

                    .setPositiveButton(R.string.yes) { _, _ ->
                        dismiss()
                        mainActivity.showDownloadDialog()
                    }
                    .setCancelable(false)
                   

                    // Create the AlertDialog object and return it
                    .create()
            dialog.setCanceledOnTouchOutside(false)
            dialog
        }
}
che10
  • 2,176
  • 2
  • 4
  • 11
  • Yes, I read about this method before asking the question, but I have `Unresolved reference: setCanceledOnTouchOutside`. I write line of code exactly where you showed and in docs I saw same code – Max Siomin Jun 17 '21 at 07:36
  • Yes I missed that. Let me fix it @Shroud228 Check the answer again – che10 Jun 17 '21 at 07:40
  • @Shroud228 We need to create it first and then set it false and then return it – che10 Jun 17 '21 at 07:42
  • What means last line "dialog". You accidentally skipped something? – Max Siomin Jun 17 '21 at 07:45
  • I was meaning to return it. I don't have access to an IDE rn, so something in the scope of let is wrong @Shroud228 Basically as I said you need to create it then set it to false and then return it. Let me try to fix it. – che10 Jun 17 '21 at 07:48