Read Table of Contents one, preface second, the meaning and use of callback three, Java implementation Interface callback four, Android interface callback v. Reference I, foreword
Recently, looking at the part of the Android fragment and activity data transfer, see the content of the interface callback, today to summarize.
ii. meaning and use of the callback 1, what is a callback.
Generally speaking, there is a certain call relationship between modules, from the way of invocation, can be divided into three types of synchronous calls, asynchronous calls and callbacks. A synchronous call is a blocking call, called by the function name of function B in function A, so that the code in the corresponding function B in memory is executed. Asynchronous invocation is a similar message or event mechanism to solve the problem of synchronization blocking, such as a notification B, they go each way, do not affect each other, do not like synchronous call, a notice B after B, must wait until the end of a, a to continue walking. A callback is a two-way invocation pattern, that is, when the invoked interface is invoked, it also invokes the other's interface, such as a to invoke b,b at the end of the execution and then call a.
2, the use of callback
Callbacks are typically used for layer collaboration, where the upper layer installs the function on the lower level, which is the callback, and the lower class triggers the callback under certain conditions. For example, as a driver, is a bottom, he received a data, in addition to complete this layer of processing work, but also will be a callback, the data to the upper level of the application layer for further processing, which is very common in layered data communication.
Third, Java implementation interface callback
In C + +, to implement a callback function, the called function tells the caller its own pointer address. But Java does not have pointer address, can not pass the address of the method, generally using the method of interface callback to implement: the implementation of an interface of the class created by the reference to the interface variables declared, then the interface variable can invoke the interface implemented by the class method.
Principle: First create a callback object, and then create a controller object, the callback object needs to be called 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 telephone number, once you complete the task, call me.
The detailed code is as follows:
1. Create a callback interface
public interface callback{public void Doevent ();}
2, create a callback interface implementation class, in this case, the employee after the work to do what is the boss to decide.
Public class boss implements callback{public void Doevent () {System.out.println ("Call the Boss, tell the work has been done");}
3, create the control class, which 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 hard ..."); Callback.doevent (); } }
4. Test class
public class testmain{public static void Main (string[] args) {//Create Controller object that will be supplied to his callback object for employee Employee=new employee (new B OSS ()); Start Controller object run Employee.dowork (); } }
Run Results screenshot:
four, the interface callback in Android
The callback mechanism in Android is heavily used. For example, in the activity defines a number of life cycle in different states to invoke the method, these methods are NULL implementation, the system framework to invoke, the user will also call to implement.
A simple example is the button's Click Response Event Implementation Mechanism
Button.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {}});
Onclicklistener is the interface of the Android system and then passes in the callback object in the application we write, so that we can achieve interface unification and achieve different results. This implementation mechanism is similar to the following code:
public class a{public void Setonclicklistener (Onclicklistener onclicklistener) {Onclicklistener.onclick (); Interface onclicklistener{public void OnClick ();}
public class B {public static void main (string[] args) {a a=new a (), A.setonclicklistener (new Onclicklistener () {public V OID OnClick () {//TODO automatically generated method stub}}); } }
Where a is equivalent to Button,a button, B is equivalent to view. v. reference materials
1, http://blog.csdn.net/bjyfb/article/details/10462555
2, http://www.jcodecraeer.com/a/chengxusheji/java/2012/0822/370.html