Android callback mechanism, android callback mechanism

Source: Internet
Author: User

Android callback mechanism, android callback mechanism

Definition:

The so-called callback means that there is a function a in a program. I call function B in this program. I want function B to call function a. This function is called a callback function. (0-0 only after reading it online for a long time)

C ++ implements callback:

For example, I call the fast sorting function qsort (a, a + n, cmp) in the program. Here, a is an array, and n is the number of array elements, cmp functions are defined in your own program. If I want qsort to call my cmp function, I need to pass the cmp function pointer to qsort so that qsort can call my cmp function. This implements the callback, and this cmp function is called the callback function.

Android callback implementation:

For JAVA, the function pointer is useless, so an interface is used to implement function callback. Let's use a Button.

A Class A implements the OnClickListener interface, which implements the onClick () method. A wants the Button to call The onClick () method of A, so it first calls the setOnClickListener (OnClickListener l) of the Button object) the method uploads the OnClickListener object of a to the Button (the Button class must have an OnClickListener reference). Through this reference, the Button can call The onClick () method of, the callback is also implemented.

I have understood it for a long time. I hope you can give me more advice.

Related Article

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.