Android_dialog cancle and dismiss differences

Source: Internet
Author: User

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
    1. public void cancel ()  {  
    2.        if   (mcancelmessage != null)  {  
    3.               
    4.             // obtain a new message  so this dialog can be re-used  
    5.             message.obtain (mcancelmessage). SendToTarget ();  
    6.        }  
    7.         dismiss ();   
    8.    }  

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
  1. Public void Setoncancellistener (final Oncancellistener listener) {
  2. if (listener! = null) {
  3. Mcancelmessage = Mlistenershandler.obtainmessage (CANCEL, listener);
  4. } Else {
  5. Mcancelmessage = null;
  6. }
  7. }
  8. Ublic void Setcancelmessage (final Message msg) {
  9. Mcancelmessage = msg;
  10. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.