1. Concept
The client program C calls a function A in the service program s, and then S, at some point, calls a function B in C, and for C, this b is called a callback function.
Generally speaking, C does not call B,c itself to provide B's purpose is to let s to invoke it, and C has to provide.
Since s does not know B name provided by C, S will contract B's interface specification (function prototype), and then by C advance through S's a function R tells S itself to use the B function, this process is called the callback function registration, R is called the registration function.
2. Give me a chestnut.
One day, I call to ask you questions, of course, is a problem, ^_^, you can not think of a solution, I can't hold the phone in there silly, so we agreed: Wait until you come up with a way to call me, so I hung off the phone to do other things. After xx minutes, my cell phone rang, you cheerfully said that the problem has been taken care of, should be treated as such. The story ends here. This example 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.
3. Example of Android
A Defining interfaces
Public interface Onclicklistener {
public void OnClick (Button b);
}
B. Defining a button
public class Button {
Onclicklistener Listener;
public void Click () {
Listener. OnClick (this);
}
public void Setonclicklistener (Onclicklistener listener) {
This.listener = listener;
}
}
C To assign an interface object Onclicklistener to the interface member of a Button
public class Activity {
Button button = New button ();
Button.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (Button b) {
System.out.println ("clicked");
}
});
Button.Click (); User Click,system call Button.Click ();
}
}
4. Application examples
Android callback function two: application example
Android back function one: basic concepts