Understanding of the Android callback mechanism

Source: Internet
Author: User

Defined:

The so-called callback means that a program has a function A, I call the B function in this program, I want the B function to call my a function, this a function is called the callback function. (Read the Internet for a long time to understand 0-0)

C + + for callback implementations:

For example, I call Qsort (A,A+N,CMP) in the program, where a is an array, n is the number of array elements, and the CMP function is defined within its own program. I want qsort to call my CMP function, so I'm going 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's implementation of callbacks:

For Java, the use of a function pointer, so the use of an interface to implement a function callback, take the button to say it.

A Class A implements the Onclicklistener interface, the onclick () method is implemented, and a wants the button to invoke the OnClick () method of a, so that the button object's Setonclicklistener is called First ( The Onclicklistener L) method passes an object that implements Onclicklistener to the button (the button class must have a onclicklistener reference), so through this reference, The button can invoke the OnClick () method of a, and the callback is implemented.

Understanding for a long time, I hope that the great God to teach you.

Understanding of the Android callback mechanism

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.