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.