Alertdialog is easy to use, but one question is: what is the difference between the dismiss method and the Cancel method?
Alertdialog Inheritance and dialog, now look at the structure diagram:
The implementation of the dismiss and cancel methods is then found in the dialog class. Important to see dismiss Source:
Java code
- public void cancel () {
- if (mcancelmessage != null) {
-
- // obtain a new message so this dialog can be re-used
- message.obtain (mcancelmessage). SendToTarget ();
- }
- dismiss ();
- }
See what you got! The dismiss method was called in the Cancel method. But now there is one more question: What is Mcancelmessage?
Private Message mcancelmessage; This is a statement in the source code.
Then look at the source code:
Java code
- Public void Setoncancellistener (final Oncancellistener listener) {
- if (listener! = null) {
- Mcancelmessage = Mlistenershandler.obtainmessage (CANCEL, listener);
- } Else {
- Mcancelmessage = null;
- }
- }
- Ublic void Setcancelmessage (final Message msg) {
- Mcancelmessage = msg;
- }
Now the problem is clear, that is, if you call Setoncancellistener when creating Alertdialog, the Mcancelmessage variable has a role, otherwise dismiss and cancel are equivalent.
Public void Cancel () SINCE:API Level 1
Cancel the dialog. This was essentially the same as calling dismiss()
, but it would also call your DialogInterface.OnCancelListener
(if registered).
Cancel the dialog box, basically the same as calling the dismiss effect. But the cancel colleague will also callDialogInterface.OnCancelListener注册的事件,如果注册了。
Public void dismiss () SINCE:API Level 1
Dismiss this dialog, removing it from the screen. This method can be invoked safely the from any thread. Note that you should not override this method to does cleanup when the dialog is dismissed, instead implement onStop()
.
Reference: http://blog.csdn.net/cpcpc/article/details/6774823
Android_dialog cancle and dismiss differences