Reputation: 21893
I am not very familiar with interfaces so i am wondering, what is the difference between passing a reference of an activity to an asynctask, then calling a method of that activity in onPostExecute. what is the difference between these those examples, why should interface be used instead
Interface example
public interface OnTaskCompleted{
void onTaskCompleted();
}
Activity:
public YourActivity extends Activity implements OnTaskCompleted{
}
AsyncTask:
public YourTask extends AsyncTask<Object,Object,Object>{
private OnTaskCompleted listener;
public YourTask(OnTaskCompleted listener){
this.listener=listener;
}
protected void onPostExecute(Object o){
listener.onTaskCompleted();
}
}
activity example
Activity:
public YourActivity extends acitivty {
public myCallbackMethod(){
//do something
}
}
AsyncTask
public YourTask extends AsyncTask<Object,Object,Object>{
private YourActivity act;
public YourTask(YourActivity act){
this.act=act;
}
protected void onPostExecute(Object o){
act.myCallbackMethod();
}
}
Upvotes: 2
Views: 70
Reputation: 18977
I just want to make above answer be clearer so:
The differences is not showing here clearly. but in general and in the world of OOP, you can find a lot of examples that make you use this fundamental principal of design pattern program to interface. here is a quick example that make you use this pattern:
consider you have an object A as a base class and you pass it to your other class B to call method f
of class A
, so in this scenario you must declare A in the header of a function of B. So obviously B only accepts A and its children. now consider you want a class called C to have a function f
and you want to call that function from B
. obviously it is not a good idea to extend C from A because they do not have any Is-A
relationship so what should you do? again declaring another function in B to accept a reference from C
. it is not a good idea because after a while you may need other object called D
to call its f method. so to prevent those situation you declare an interface and make every class to implement that, in this scenario all you have to declare in object B is a method that accept that interface as a reference. every object that is implements that interface can pass to your B class.
now consider you want to create a library and you need to call a method from your user object. for example calling a method that the requested image has been downloaded successfully. if you accept the reference of activity user must just use your code in the activity and can not use it from services. so you must provide different functions that accept services, activity, fragment and so on. instead of that you just say I accept only this interface, every one wants to use implement that.
Upvotes: 1
Reputation: 83537
This is primarily a matter of taste and design style. The interface provides the advantage that you are not required to send an Activity
to the AsyncTask
. Instead you can choose to create a separate class which implements the interface.
Upvotes: 4