Callback mechanism in the Android monitor user interface operation of the embodiment
This article discusses the following two items:
1. Callback function
2. Callback mechanism in the Android framework to monitor the user interface operation of the role
A callback function
A callback function is a function that is called through a function pointer. If you pass the pointer (address) of the function as an argument to another function, when the pointer is used to invoke the function it points to, we say this is a callback function. The callback function is not called directly by the implementing party of the function, but is invoked by another party when a particular event or condition occurs, and is used to respond to the event or condition.
There is no concept of pointers in Java, and the functions of callbacks are implemented through interfaces and inner classes:
1. Define the interface Callback, including the callback Method Callback ()
2. Declare a callback interface object in a class caller Mcallback
3. The interface member (MCALLBACK) that is assigned to the caller object in the program is an inner class object such as
New Callback () {
Callback () {
The concrete implementation of the function
}
This allows the Mcallback interface member of the caller object to invoke the callback () method, when needed, to complete the callback.
Second, the callback mechanism in the Android Framework monitoring user interface operation role
The Android event listener is the interface of the view class and contains a separate callback method. These methods are called by the Android framework when a listener registered in the view is triggered by a user interface action. The callback method is included in the Android Event listener interface:
For example, the View object of Android contains a member variable named Onclicklistener interface, and the user's click action will be given to Onclicklistener's OnClick () method for processing.
If the developer needs to handle the Click event, you can define a Onclicklistener interface object to assign to the interface member variable onclicklistener of the view that needs to be clicked, typically with the view Setonclicklistener () function to complete this operation.
When a user clicks an event, the system will call back the OnClick () method of the Onclicklistener interface member that was clicked on view.
Instance (for the Emulation of button click event Listener on Android interface):
1. Defining interfaces
Public Interface Onclicklistener {
Public void OnClick (Button b);
}
2. Define Button
Public class Button {
Onclicklistener Listener;
Public void Click () {
Listener. OnClick (this);
}
Public void Setonclicklistener (Onclicklistener listener) {
this. Listener = listener;
}
}
3. To assign an interface object Onclicklistener to the interface member of a button
Public class Activity {
Public Activity () {
}
Public Static void Main (string[] args) {
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 ();
}
}
Switch to an easy-to-understand Android callback implementation---> (RPM)