I was wondering if I could change the way my method function, similar to Blocks iOS.
So I have this interface create in class API.java
public interface APIListener {
void apiResponseSuccess(String output);
void apiResponseFailed(String output);
}
public APIListener listener = null;
public void myMethod{
listener.apiResponseSuccess("output");
}
In order to call my interface created, i have to implements
API.APIListener
. and override the functions
@Override
public void apiResponseSuccess(Object output) {
Log.i("output from api",(String) output);
}
@Override
public void apiResponseFailed(String output) {
}
And to call it, I have to use :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
API api = new API();
api.listener = this;
api.myMethod();
}
But drawbacks using this, It's hard to maintain if I call many methods inside the API, because all the results will go to apiResponseSuccess
in my class, and have to tag which one coming from. Where the iOS comes with Blocks, it becomes easier. so basically, Is there a way to return the interface methods direct when we call it. similar to this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
API api = new API();
api.listener = this;
api.myMethod(
public void apiResponseSuccess(Object output) {
Log.i("output from api",(String) output);
}
public void apiResponseFailed(String output) {
}); //so the result will go in separately inside the where the function is called.
}