Use of callback methods
1, Introduction: Program A exposed a method a out. In the B program call the A method, B to implement the corresponding logic.
Instance:
1), define an interface
Public interface IAA {
public void message ();
}
2) Define a class that exposes the show method in the class.
public class AA {
Private IAA IAA;
public void Addlistner (IAA IAA) {
THIS.IAA=IAA;
}
public void Show () {
This.iaa.message ();
}
}
3), define BB class
3.1),
public class BB {
public static void Main (string[] args) {
AA aa=new AA ();
Anonymous inner class
Aa.addlistner (New IAA () {
B program to implement its own logic code
@Override
public void message () {
System.out.println ("Adsfasdf");
}
});
Aa.addlistner (New BB ());
Call the method exposed by a program.
Aa.show ();
}
}
3.2),
public class BB implements iaa{
@Override
public void message () {
System.out.println ("bbbbbbbb");
}
}
public static void Main (string[] args) {
AA aa=new AA ();
Aa.addlistner (New BB ());
Call the method exposed by a program.
Aa.show ();
}
Note: The events that are added to Android can be very good for the use of callback methods or for fragment and activity communication.
Ajax access to the server and so on are used to the callback function.
Use of the Java callback method