Alertdialog the difference between dismiss and cancel methods
Alertdialog is easy to use, but one question is: what is the difference between the dismiss method and the Cancel method?
Today has the time, looked at the source code (actually the source code is not all so abstruse!) ~ ~).
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< Span style= "color:black;" > 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.
Android Development--diglog cancel differs from dismiss method