I have an issue where I need to access a method in my Activity from Android AsyncTask’s onPostExecute() method
I have 2 Activities both contain a common method as below:
(1) Activity1 — > refreshUI()
(2) Activity2 —-> refreshUI()
I got one AsyncTask call GetDataAsyncTask(Activity a ) which takes calling activity as argument
Now from my activity1 I will call new GetDataAsyncTask(Activity1.this).execute.
Same as above from my activity2 I will call new GetDataAsyncTask(Activity2.this).execute.
My AsyncTask is as below :
public class GetDataAsyncTask extends AsyncTask<String ,Void , String> {
public Activity context;
public PostAsyncTaskHelper(Activity c) {
context = c;
}
protected String doInBackground(String... arg0) {
// Webservice calls
}
protected void onPostExecute(String result) {
if(result.equals("qq")) {
//Where I am not able to access refreshUI()
//method of any one of my activities
context.refreshUI()
}
}
}
Can anyone help me how to get reference of any of the called activities from AsyncTask?
Define an interface for your Activities
your Activities must be defined as
implements MyActivityRefreshInterface.Your onPostExecute can then cast the context as (MyActivityRefreshInterface)context