Callback in android
1. Introduction
The most classic callback in android is the click event setting listener (usually through switch (v. getId ().
btn_rigister.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // TODO log in }});
View exposes an interface onClick.
public interface OnClickListener { /** * Called when a view has been clicked. * * @param v The view that was clicked. */ void onClick(View v); }
The current Actvity does not perform any operations on this method when we use the specific method. The onClick method after the enrichment is called back by the view! For example
public boolean callOnClick() { ListenerInfo li = mListenerInfo; if (li != null && li.mOnClickListener != null) { li.mOnClickListener.onClick(this); return true; } return false; }
For more information, see View. java.
2. What is callback?
Callback is actually a two-way call mode. That is to say, when an interface is called, the caller will also call the interface of the other party. It sounds like a complete interface, translation is "an example that implements the abstract class/interface to implement the abstract method provided by the parent class, and then return the method to the parent class for processing". I read it three times, can you understand the meaning?
Make it clear that the implementation method is returned to the parent class that provides the interface!
3. Why callback?
This shows the java Object-Oriented "Everything is an object". We need to abstract the features of common objects, and there are characteristics in common, each different feature needs to be handed over to a specific situation for processing. by exposing the interface method, you can reduce a lot of repetition, and the code is more elegant.
For example, a view has the ability to be clicked, but each click produces a different event processing. Therefore, android exposes an interface with an onClick method. If you need to handle it, you can write it, view only calls this method. You have already processed different click events.
4. How to Write
A good little chestnut
Define an Interface: defines an Interface in a class and an abstract method in the Interface.
public interface Callback{ public abstract void work(); }
[Interface object] defines a member variable for this interface in the class
private Callback mCallback;
[Set object] defines a public method in the class. You can set the object of this interface and call this method to assign values to the interface object variable.
public void setCallback(Callback callback) { this.mCallback = callback; }
[Call method] call a method in an interface object
public void doWork() { mCallback.work(); }
OK,
The complete code is as follows:
Public class Employee {/** define the Callback interface member variable */private Callback mCallback;/** declare the Callback interface */public interface Callback {public abstract void work ();} /** set the Callback interface object member variable */public void setCallback (callback Callback) {this. mCallback = callback;}/** call the method in the callback interface object */public void doWork () {mCallback. work ();}}
Then we just need to use it.
Public class Boss {private Employee employee;/** set a callback function for the Employee. The specific callback Method */public void setCallback () {employee is defined here. setCallback (new Employee. callback () {@ Override public void work () {System. out. println ("work ");}});}}