I have the following asynctask class which is not inside the activity. In the activity I’m initializing the asynctask, and I want the asynctask to report callbacks back to my activity.
Is it possible? Or does the asynctask must be in the same class file as the activity?
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
caller.sometextfield.setText("bla");
}
Something like this?
You can create an
interface, pass it toAsyncTask(in constructor), and then call method inonPostExecute()For example:
Your interface:
Your Activity:
And your AsyncTask:
EDIT
Since this answer got quite popular, I want to add some things.
If you’re a new to Android development,
AsyncTaskis a fast way to make things work without blocking UI thread. It does solves some problems indeed, there is nothing wrong with how the class works itself. However, it brings some implications, such as:Activity, it will stay in memory even after user left the screen (or rotated the device).AsyncTaskis not delivering result toActivityifActivitywas already destroyed. You have to add extra code to manage all this stuff or do you operations twice.ActivityWhen you feel that you matured enough to move on with Android, take a look at this article which, I think, is a better way to go for developing your Android apps with asynchronous operations.