Passing data to AsyncTask: execute (args) vs constructor?

I use a lot of AsyncTask

s in my projects . They all send some data via http to the server. In some of them I am passing vie data execute( data... )

. In others, I use the constructor for this:

new SomeTask().execute( 1, 2, 3 );

      

or

new SomeOtherTask( 1, 2, 3 ).execute();

      

Basically, these calls are equivalent, as AsyncTask

they cannot be reused and are stateless.

What is the preferred way to use them? Did I miss something?

+3


source to share


2 answers


In my opinion, pass parameters to the method execute

(if they are all of the same type, off course). Using a custom constructor forces you to write it and store the parameters in private variables, which is useless unless those parameters are used in the method OnPostExecute

.



0


source


Use new SomeTask().execute( 1, 2, 3 );

It's better than passing values ​​to a constructor. If you pass your input as an argument to a constructor, you will need to create member variables, and this would be overkill since AsyncTask allows you to pass variable arguments in the run method.



Unless there is a need to maintain member variables for the input passed to the AsyncTask, the constructor should not be used for the input.

0


source







All Articles