ragingcorgi
ragingcorgi

Reputation: 3468

Android: What happens if AsyncTask execute() is called while the task is running?

Say there is an AsyncTask that is triggered from a UI button, and the button click triggers execute() to start the task. What happens if the task is in the middle of execution, and user calls execute() again?

I know this can be prevented by checking AsyncTask.getStatus(), but what happens if I don't want to check that?

Upvotes: 1

Views: 324

Answers (1)

T D Nguyen
T D Nguyen

Reputation: 7603

Due to AsyncTask doc:

There are a few threading rules that must be followed for this class to work properly:

The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.

  1. The task instance must be created on the UI thread. execute(Params...) must be invoked on the UI thread.
  2. Do not call onPreExecute(), onPostExecute(Result), oInBackground(Params...), onProgressUpdate(Progress...) manually.
  3. The task can be executed only once (an exception will be thrown if a second execution is attempted.)

Hope this help!

Upvotes: 2

Related Questions