Simple and easy to understand android callback implementation

 Reflection of callback mechanism in Android monitoring user interface operation

 

This article discusses the following two contents:
1. Callback function
2. The role of callback mechanism in monitoring user interface operations of the Android framework


1. Callback function: A
       callback function is a function called through a function pointer. If you pass a pointer (address) of a function as a parameter to another function, when this pointer is used to call the function it points to, we say this is a callback function. The callback function is not called directly by the implementer of the function, but is called by another party when a specific event or condition occurs to respond to the event or condition.
There is no concept of pointers in Java, and the function of callback is realized by means of interfaces and inner classes:
1. Define the interface Callback, including the callback method callback()
2. Declare a Callback interface object mCallback in a class Caller
3. Give it in the program Interface member of the Caller object (mCallback) An inner class object such as
new Callback(){
     callback(){
         //Concrete implementation of the function
     }
, when needed, the callback() method can be called with the mCallback interface member of the Caller object to complete callback.


 2. The role of the callback mechanism in the Android framework's monitoring of user interface operations: The
Android event listener is an interface of the View class, including a separate callback method. These methods will be called by the Android framework when a listener registered in the view is triggered by a UI action. The callback method is included in the Android event listener interface:
For example, Android's view object contains a member variable named OnClickListener interface, and the user's click operation will be handed over to the OnClick() method of OnClickListener for processing.
If the developer needs to process the click event, he can define an OnClickListener interface object and assign it to the interface member variable OnClickListener of the view that needs to be clicked. Generally, the setOnClickListener() function of the view is used to complete this operation.
When there is a user click event, the system will call back the OnClick() method of the OnClickListener interface member of the clicked view.
Example (simulation of Button click event monitoring on Android interface):
1. Define the interface
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. 将接口对象OnClickListener 赋给 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();
  }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326933019&siteId=291194637