Reputation: 37
Can i call the same asynctask depending on some conditions?
for example:
switch(item)
{
case 1:
dataCommunicator.execute();
break;
case 2:
dataComminicator.exwcute();
break;
.
.
.
}
class dataCommunicatoe extends AsyncTask{
.
.
.
.
.
}
Upvotes: 1
Views: 284
Reputation: 9904
Yes it is possible:
You can call the same async task by passing parameters as below:
if (someLogicIsTrue)
new AsyncTaskOperation().execute("FETCHUSER");
else
new AsyncTaskOperation().execute("VALIDATECREDENTIALS");
Async Task should be:
private class AsyncTaskOperation extends AsyncTask <String, Void, Void>
{
String paramObject = "";
@Override
protected Void doInBackground(String... paramsObj) {
paramObject = paramsObj[0];
if ("FETCHUSER".equals(paramObject))
{
// OPERATION FOR FETCH USER
}
else if ("VALIDATECREDENTIALS".equals(paramObject))
{
// OPERATION FOR VALIDATE CREDENTIALS.
}
return null;
}
}
Upvotes: 1
Reputation: 6054
Yes, a good approach is to pass an interface to a custom AsyncTask, so you can decide what to do when it reaches "OnPostExecute" and reuse the same custom AsyncTask for multiple fragments/activities.
Upvotes: 0