Skip to content

Commit 7f02dc5

Browse files
committed
Add setOnDismissListener to AlertDialog.Builder
Add documentation to setOnCancelListener to clarify that the cancel event plus the events for the various choice buttons are not the exhaustive set of ways the dialog can be dismissed, and that a dismiss listener should be used if the app needs to cover all cases of dismissal. Change-Id: I9e9d6f90f6f9ccaeb2c697474ab353e2d78f37b9
1 parent e45ca31 commit 7f02dc5

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

api/current.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3012,6 +3012,7 @@ package android.app {
30123012
method public android.app.AlertDialog.Builder setNeutralButton(int, android.content.DialogInterface.OnClickListener);
30133013
method public android.app.AlertDialog.Builder setNeutralButton(java.lang.CharSequence, android.content.DialogInterface.OnClickListener);
30143014
method public android.app.AlertDialog.Builder setOnCancelListener(android.content.DialogInterface.OnCancelListener);
3015+
method public android.app.AlertDialog.Builder setOnDismissListener(android.content.DialogInterface.OnDismissListener);
30153016
method public android.app.AlertDialog.Builder setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener);
30163017
method public android.app.AlertDialog.Builder setOnKeyListener(android.content.DialogInterface.OnKeyListener);
30173018
method public android.app.AlertDialog.Builder setPositiveButton(int, android.content.DialogInterface.OnClickListener);

core/java/android/app/AlertDialog.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,14 @@ public Builder setCancelable(boolean cancelable) {
566566

567567
/**
568568
* Sets the callback that will be called if the dialog is canceled.
569+
*
570+
* <p>Even in a cancelable dialog, the dialog may be dismissed for reasons other than
571+
* being canceled or one of the supplied choices being selected.
572+
* If you are interested in listening for all cases where the dialog is dismissed
573+
* and not just when it is canceled, see
574+
* {@link #setOnDismissListener(android.content.DialogInterface.OnDismissListener) setOnDismissListener}.</p>
569575
* @see #setCancelable(boolean)
576+
* @see #setOnDismissListener(android.content.DialogInterface.OnDismissListener)
570577
*
571578
* @return This Builder object to allow for chaining of calls to set methods
572579
*/
@@ -575,6 +582,16 @@ public Builder setOnCancelListener(OnCancelListener onCancelListener) {
575582
return this;
576583
}
577584

585+
/**
586+
* Sets the callback that will be called when the dialog is dismissed for any reason.
587+
*
588+
* @return This Builder object to allow for chaining of calls to set methods
589+
*/
590+
public Builder setOnDismissListener(OnDismissListener onDismissListener) {
591+
P.mOnDismissListener = onDismissListener;
592+
return this;
593+
}
594+
578595
/**
579596
* Sets the callback that will be called if a key is dispatched to the dialog.
580597
*
@@ -917,6 +934,7 @@ public AlertDialog create() {
917934
dialog.setCanceledOnTouchOutside(true);
918935
}
919936
dialog.setOnCancelListener(P.mOnCancelListener);
937+
dialog.setOnDismissListener(P.mOnDismissListener);
920938
if (P.mOnKeyListener != null) {
921939
dialog.setOnKeyListener(P.mOnKeyListener);
922940
}

core/java/android/app/Dialog.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,12 @@ public void cancel() {
11061106

11071107
/**
11081108
* Set a listener to be invoked when the dialog is canceled.
1109-
* <p>
1110-
* This will only be invoked when the dialog is canceled, if the creator
1111-
* needs to know when it is dismissed in general, use
1112-
* {@link #setOnDismissListener}.
1109+
*
1110+
* <p>This will only be invoked when the dialog is canceled.
1111+
* Cancel events alone will not capture all ways that
1112+
* the dialog might be dismissed. If the creator needs
1113+
* to know when a dialog is dismissed in general, use
1114+
* {@link #setOnDismissListener}.</p>
11131115
*
11141116
* @param listener The {@link DialogInterface.OnCancelListener} to use.
11151117
*/

core/java/com/android/internal/app/AlertController.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ public static class AlertParams {
751751
public DialogInterface.OnClickListener mNeutralButtonListener;
752752
public boolean mCancelable;
753753
public DialogInterface.OnCancelListener mOnCancelListener;
754+
public DialogInterface.OnDismissListener mOnDismissListener;
754755
public DialogInterface.OnKeyListener mOnKeyListener;
755756
public CharSequence[] mItems;
756757
public ListAdapter mAdapter;

0 commit comments

Comments
 (0)