English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes the usage of anonymous inner class and callback function in Android programming. Share it with everyone for reference, as follows:
In Android development, we often use some anonymous inner classes and their callback functions, for example, when we set a listener for Button, we usually implement the OnCLickListener interface and override the Onclick() method within it, which is the anonymous inner class and callback function. So how much do you understand about Java anonymous inner class and callback function? This document gives a detailed explanation.
First of all, it should be known that inner classes are a major addition to the JAVA language. Inner classes can almost be placed in any position within a class, can be at the same level as instance variables, or within a method, or even part of an expression. Java's inner classes are divided into member inner classes, local inner classes, and anonymous inner classes. Let's talk about anonymous inner classes below.
A. About callback function
Anonymous inner classes use callback functions, what is a callback function?
What is called callback is that the client program C calls a function A in the service program S, and then S calls a function B in C at some point, for C, this B is called a callback function. For example, Win32The window procedure function under the window is a typical callback function. Generally speaking, C does not call B itself, the purpose of C providing B is to allow S to call it, and it is necessary for C to provide it. Since S does not know the name of B provided by C, S will agree on the interface specification (function prototype) of B, and then C will inform S in advance through a function R that it will use the B function, which is called the registration of the callback function, and R is called the registration function. Web Service and Java's RMI both use callback mechanisms to access remote server programs. The following is an example to illustrate this:
Programmer A wrote a program (program a), in which a callback function interface is reserved and the program is encapsulated. Programmer B wants a to call a method in his own program b, so he calls his own method in b through the interface in a. The purpose is achieved. In C/C++In this case, a callback function is needed, and the callback function needs to tell the caller its pointer address, but in JAVA there are no pointers, what should we do? We can implement the definition of callback function through interface (interface).
Assuming I am programmer A, here is my program a:
public class Caller { public MyCallInterface mc; public void setCallfuc(MyCallInterface mc) { this.mc= mc; } public void call(){ this.mc.method(); } }
I still need to define an interface so that programmer B can write a program to implement the interface according to my definition.
public interface MyCallInterface { public void method(); }
Therefore, programmer B only needs to implement this interface to achieve the purpose of callback:
public class B implements MyCallInterface { public void method() { System.out.println("callback"); } public static void main(String args[]) { Caller call = new Caller(); call.setCallfuc(new B()); call.call(); } }
B. About anonymous inner class
To understand what is an inner class, the role of inner class, Java's polymorphism, and what is a callback function. There are many detailed explanations on the Internet, but for simplicity, only a brief explanation is provided. Anonymous inner classes are passed as function parameters to methods, where the interface type accepts the passed anonymous class, and then calls its method, which is polymorphism. The principle is actually to implement callback, because it will call the method in your anonymous inner class. The following code is hand-written and has not been tested, just for demonstration purposes.
interface InterfaceA{ String go(); } class Test{ public void prtGo(InterfaceA ia){ System.out.println(ia.o()); } public static void main(String []args){ Test t = new Test(); t.prtGo(new InterfaceA(){ public String go(){ return"go"; } }); } }
Readers who are interested in more about Android-related content can check the special topics on this site: 'Android Development Tutorial for Beginners and Advanced Users', 'Summary of Android Debugging Skills and Common Problem Solving Methods', 'Summary of Android Multimedia Operation Skills (audio, video, recording, etc.)', 'Summary of Android Basic Component Usage', 'Summary of Android View View Skills', 'Summary of Android Layout Layout Skills', and 'Summary of Android Control Usage'.
Hoping the content described in this article will be helpful to everyone's Android program design.
Declaration: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, does not undergo manual editing, and does not assume relevant legal liabilities. If you find content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)