The result is passed from doInBag() to onPostexecute() method of AsyncTask.
Now there you can use Handlers to send data back to your activity like this:
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (mProgressDialog != null) {
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
final Message message = new Message();
if (result != null && !result.equals("")) {
if (result.equals("success")) {
message.what = 1000; // to show SUCCESS
} else {
message.what = 1001; // to show FAILURE
}
} else {
message.what = 1001; // to show FAILURE
}
mHandler.sendMessage(message);
}
EDIT: The use of handlers with AsyncTask:
declare a Handler in your activity like this:
/**
* Handler to update the UI.
*/
private static final Handler sHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1000) {
} else {
// failure
}
}
};
Now in your activity, call the AsyncTask like this:
YourAsyncTask obj = new YourAsyncTask(context, sHandler);
obj.execute();
Now in your AsyncTask, create a Constructor like this:
public YourAsyncTask(final Context context, final Handler handler) {
this.context = context;
mHandler = handler;
}
Explanation:
Why you should use a handler here because, you may want to get back to your activity after finishing the background operation to do other task based on the AsyncTask result. Here you pass a reference of the Handler to your asyncTask, and eventually sends the result back to your activity.
Please note that, the Handler above is a static handler, otherwise a memory leak may occur if your activity finishes before completion of your async operation.