Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle AlertDialog close event?

Is there a callback when an AlertDialog is closed using the touch outside event? I have this enabled "setCanceledOnTouchOutside(true)". I want to set a visibility of a specific view once the AlertDialog is closed using the touch outside event. Any ideas?

I am using the AlertDialog.Builder() inside a Fragment if that helps.

like image 309
Aivan Monceller Avatar asked Jan 29 '14 17:01

Aivan Monceller


3 Answers

an OnDismissListener seems most appropriate, but it requires a relatively high API level, so you might need to stick with an OnCancelListener instead.

like image 70
NasaGeek Avatar answered Sep 28 '22 05:09

NasaGeek


I post Kotlin version because i was looking for it today and i had hard time

val builder = AlertDialog.Builder(requireContext())

builder.setTitle("title")
builder.setMessage("message")

builder.setOnCancelListener {  func->Log.i("meh","meh") }
like image 39
thbglbwsk Avatar answered Sep 28 '22 07:09

thbglbwsk


If you are within custom dialog class, and wish to catch 'clicked outside dialog' event - override cancel(). If you wish to catch any'dialog closed' event - override dismiss(). I recommend inserting logic BEFORE super.dismiss(). Kotlin example:

override fun dismiss() {
    Utils.hideKeyboard(mContext, window)
    super.dismiss()
}
like image 36
SilverTech Avatar answered Sep 28 '22 07:09

SilverTech