Simple understanding of callbacks in Android

Source: Internet
Author: User

First, the most abstract form-2 classes, Class A and Class B. Class A contains 1 interfaces, an interface variable, (possibly containing) 1 methods for assigning values to interface variables, and 1 "places" that use interface variables; Class B implements the interface in a, (possibly) contains a reference to 1 Class A instances, and (possibly, a method that assigns a value to an interface variable in Class A) passes "itself" to an interface variable of Class A.

And then a little story:

What is the first thing we do when we answer the exam? Yes, it's a school number and a name. Notice here, we fill in the number and name is not for ourselves to see (that the method is not for themselves to call), but to the teacher to register the score (pre-leave the system for future calls), which is actually a callback application. The teacher provides the interface (enter the name, number rules), we use the interface registration. See the small story above, we have some understanding of the callback, we go back to the beginning of the small story. The story illustrates the programming pattern of asynchronous + callback. Which, you later on the phone to tell me the result is a "callback" process; my mobile phone number must be told you before, this is the registration callback function, my mobile phone number should be valid and the phone can receive your call, this is the callback function must conform to the interface specification

First, Class A.

 Public classa{ ...//1 Interface Variables    PrivateCallback Mcallback; ...    //1 Interfaces     Public Interfacecallback{voiddosomething (); }    ...    //a method for assigning a value to an interface variable     Public voidSetcallback (Callback Callback) {mcallback=callback; }    ...    //a place to use interface variables     Public voidOnExecute () {... mcallback.dosomething ();    ...    } ...}

Then a Class B.

 Public class Implements a.callback{...     // Reference to an instance of Class A    Private A mainstance;    ...     // Class B implements an interface    of Class A  Public void dosomething () {        log.d ("TAG", "would do something");    }    ...     // Class B passes itself (actually the implementation of the interface) to the interface variable of the Class A instance    Mainstance.setcallback (this);       }

A few points below:

    • There is an interface variable and an interface in Class A.
    • Class B implements an interface of Class A (this interface is called a callback interface).
    • An interface variable of Class A (through some method) obtains an interface that is implemented by Class B.
    • A class "uses" This interface variable at an appropriate time, that is, a function in the calling interface (the function is called a callback function).

With the things in life, in fact, it is like someone buys a killer to kill the enemy = =, a just tell the killer kill this purpose, specifically how to kill a foe, by the killer to decide. This is a class A, the killer is Class B, a in a moment to tell the killer kill is a class call callback interface inside the callback function, Killer Kill method is Class B implementation of Class A callback interface ...

C does not call B,c itself to provide B's purpose is to let s to invoke it, and C has to provide. s does not know what B is provided by C, so s will contract the interface specification of B (function prototype), and then by C advance through S a function R tells S itself will use B function (that is, registration). R is a registered function.

In simple terms:

A callback function is a function reserved for a system call, and we often know when the function is called

A detailed description:

  

A similar little story: What is the first thing we do in our exams? Yes, it's a school number and a name. Notice here, we fill in the number and name is not for ourselves to see (that the method is not for themselves to call), but to the teacher to register the score (pre-leave the system for future calls), which is actually a callback application. The teacher provides the interface (enter the name, number rules), we use the interface registration. See the small story above, we have some understanding of the callback, we go back to the beginning of the small story. The story illustrates the programming pattern of asynchronous + callback.  Which, you later on the phone to tell me the result is a "callback" process; my mobile phone number must be told you before, this is the registration callback function, my mobile phone number should be valid and the phone can receive your call, this is the callback function must conform to the interface specification. Well, we've probably got the basic flow of callbacks, and here's a look at the basic use of callbacks in Android.
Button button = (Button)this. Findviewbyid (R.id.button);  Button.setonclicklistener (Newbutton.onclicklistener () {    /// callback  function @override  Publicvoidonclick (View v) {  buttontextview.settext ("button clicked");  }  );  

The above code adds an event listener to the button, which is actually one of the most common scenarios for "callbacks". We do not explicitly call the OnClick method ourselves. After the user triggers the button's Click event, it is automatically called by the Android system.

Reference from: http://blog.csdn.net/lindir/article/details/7819720,http://blog.csdn.net/chengkun_123/article/details/51318379

Simple understanding of callbacks in Android

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.