Callback (ruin?) ) mechanism
Http://www.cnblogs.com/heshuchao/p/5376298.html This article is very good to say.
This is a Java category, except that we use the OnClick event of the Android button to explain the mechanism .
First, we're going to create an interface that declares the method we want to invoke in this interface. (an abstract method can be defined in an interface, specifically by a specific class to implement the method in the interface)
Pause, for a chestnut, we all know to add the listener to the button to override the OnClick method, then the code written in this onclick is different, then when we click on the button to make a different response (special, it sounds like a silly than the look??), this is the purpose of the callback mechanism , now we begin to introduce the concept of a callback mechanism:
Online a lot of what program a calls program B in method C, and then method C in turn calls the method in program a D; is not confused, in fact, probably the mechanism is almost like this, finally made clear, found in practice does not seem to be exactly the same; Yes, I was also a face of the initial??。
So what is a callback: in a class need to use some methods, but these methods are exactly what kind of method, it is still unclear, here the smart classmate must have Lenovo interface, interface is defined a series of abstract methods, these abstract methods by those who use the interface specific classes to provide implementation, my guess is The Onclicklistener in Button.setonclicklistener (new View.onclicklistener) is an interface in the View class that has an abstract method onclick
That's why we're so many times.
This is in fact the use of an anonymous inner class to implement the OnClick method in the interface and to implement a reference to an instance of the method's anonymous class as a parameter passed to Setonclicklistener.
Then, in the view class, click listen for property 1 public onclicklistener Monclicklistener; It also holds the reference when we click on the button Program calls the Monclicklistener.onclick in view, then the view program in turn calls the program we write the OnClick method, also completed a so-called callback process, in the process of the onclick this method is the callback method. The author's limited ability to express, temporarily can only be said to look like this, the latter has more profound experience will be back to update this article, beginners can see the beginning of the author recommended that blog, the writing of gradual and very good understanding.
This article may exist one-sided or the problem, has the better suggestion or the opinion the author welcomes everybody in the comment area message.
Thinking about the callback mechanism of Android Click event