Reputation: 41
Is there a way to find out which activity is calling AsyncTask
? I have two activities which are calling it and I want to do different stuff in onPostExecute()
, depending on which activity is calling it.
Something like that:
protected void onPostExecute(String result) {
//do some stuff with result
if (callingActivity == mainActivity) {
//do something
} else {
//do something else
}
}
Upvotes: 0
Views: 37
Reputation: 200020
Just have your Activities subclass your CommonAsyncTask
, overriding onPostExecute()
:
new CommonAsyncTask() {
@Override
protected void onPostExecute(String result) {
// Execute the common code in `CommonAsyncTask`
super.onPostExecute(result);
// Now execute the code unique to this activity
}
};
Upvotes: 1
Reputation: 420
We always have instanceof for the rescue. Capture the callingActivity while creating AsyncTask and use it in onPostExecute.
For example,
There are two Activities ActivityA and ActivityB. You can check in onPostExecute like
class AsyncTask<..,..,..> {
private Activity a;
public AsyncTask(Activity a) {
this.a = a;
}
protected void onPostExecute(String result) {
//do some stuff with result
if (a instanceof ActivityA) {
//do something
} else if (a instanceof ActivityB) {
//do something else
}
}
}
Upvotes: 1