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?
source to share
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.
source to share