It's kind of a Callback concept. You can implement and use the interface of Java. You'll understand if you look at the structure of setOnClickListener, which is the easiest example.
Here's a simple example of Strings moving from ClassB to callback once again.
// Callback interface
public interface CustomCallback {
public void onCall(String s);
}
// Class with callback declaration
public class ClassB {
CustomCallback mCallback;
public ClassB(CustomCallback callback) {
this.mCallback = callback;
}
public void callMe() {
System.out.println("ClassB Call");
if(mCallback !=null){
String data = "original data";
System.out.println(data);
mCallback.onCall(data);
}
}
}
// Actual Execution Part
ClassB b = new ClassB(new CustomCallback() {
@Override
public void onCall(String s) {
// This is where you handle the data transferred to the callback. The interface hands over the String, so this guy will come over.
System.out.println(s + "processed.");
}
});
b.callMe();
When running
ClassB Call original data Original data processed.
The result will come out like this~
© 2024 OneMinuteCode. All rights reserved.