0

I am using below code to get result from Asynctask

Ui thread
.....
....
    int result = new MyCustomTask().execute().get();

    if (result == 1) {
    }
    else {
    }
----
----

But my doubt is "Whats the point of having an async task when get() waits for it to complete"

GyaniPundit
  • 133
  • 1
  • 8

1 Answers1

4

No point in calling get().

Just have

new MyCustomTask().execute();

Calling get() blocks the ui thread waiting for the result. This makes AsyncTask no more Asynchronous.

The result of doInbackground computation is param to onPostExecute. So you can retrun result in doInbackground and update ui in onPostExecute.

You can use onPostExecute() to update ui if its a inner class of Activity or you can use interface as a callback to the activity to get the result.

Use interface as a callback

How do I return a boolean from AsyncTask?

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256