First, preface
Recently, looking at the data transfer of Android fragment and activity, I saw the content of the interface callback, to summarize today.
Second, the meaning and purpose of the callback 1, what is a callback?
In general, there is a certain call relationship between the modules, which can be divided into three types of synchronous calls, asynchronous calls and callbacks from the invocation mode. A synchronous call is a blocking call that is invoked in the function body of function A by writing the function name of function B so that the code of the corresponding function B in memory is executed. Asynchronous invocation is a similar message or event mechanism to solve the problem of synchronous blocking, such as a notification B, they go each way, do not affect each other, do not like synchronous call,a notification B, must wait until B after the end ofA to continue to go . A callback is a two-way invocation pattern, which means that the called interface is called when the interface is called, such as a to call b,b after execution and a.
2, the use of callbacks
Callbacks are typically used for inter-layer collaboration, where the upper layer installs the function on the lower level, which is the callback, and the lower layer triggers the callback under certain conditions. For example, as a driver, is a bottom, when he received a data, in addition to complete the processing of this layer, will also be a callback, the data to the upper layer of the application to do further processing, which is common in hierarchical data communication.
Third, Java implementation Interface callback
To implement a callback function, the called function will tell the caller its own pointer address in C + +. However, Java does not have a pointer address, can not pass the address of the method, generally adopt the method of interface callback: To assign a reference to the object that implements the class of an interface to the interface variable declared by the interface, then the interface variable can invoke the method of the interface implemented by the called Class.
Principle: First create a callback object, and then create a controller object, the callback object needs to be called the method to tell the controller object, the controller object is responsible for checking whether a scene or a condition is satisfied, when satisfied, automatically call the callback object method.
For example, boss A to employee B said, I now give you a task, and I give you my phone number, you once completed the task to call me.
The detailed code is as follows:
1. Create a callback interface
Public Interface callback{ publicvoid doevent ();}
2, create the callback interface implementation class, in this example, the employee after the job to do what is the boss to decide.
Public class Implements callback{ publicvoid doevent () { System.out.println ("Call the Boss, Informed that the work has been completed ");} }
3, create the control class, that is, the employee object in this example, he wants to hold the boss's address (that is, callback interface)
Public class employee{ CallBack CallBack; Public Employee (CallBack CallBack) { this. callback=callBack; } Public void doWork () { System.out.println ("Working in the work ...."); Callback.doevent (); }}
4. Test class
Public class testmain{ publicstaticvoid main (string[] args) { / / Create a controller object, pass the callback object provided to him into employee employee=New employee(new Boss ()) ; // start the Controller object to run employee.dowork (); }}
Operation Result:
Iv. interface Callbacks in Android
The callback mechanism is used extensively in Android. For example, in the activity defines a lot of life cycle of different states to invoke the method, these methods are empty implementations, the system framework to invoke, the user will be called to implement.
A simple example is the click-Response event Implementation mechanism of a button
Button.setonclicklistener (new Onclicklistener () { @Override public void OnClick (View v) { } });
Onclicklistener is the interface that the Android system is about, and then we pass in the callback object in the application we write, so we can achieve the interface unification and achieve different effects. This implementation mechanism is similar to the following code:
public class a{ public void Setonclicklistener (Onclicklistener Onclicklistener) {Onclicklistener.onclick (); public interface Onclicklistener { public void OnClick (); }}
Public class B { publicstaticvoid main (string[] args) { a a=new A (); A.setonclicklistener (new Onclicklistener () { publicvoid OnClick () { // TODO Auto-generated method stub } } }
Where a is equivalent to Button,a button, Class B equals view.
V. References
1, http://blog.csdn.net/bjyfb/article/details/10462555
2, http://www.jcodecraeer.com/a/chengxusheji/java/2012/0822/370.html
Java Interface callback mechanism