AsyncTask with one or more methods

2

I would like to know what are the advantages and disadvantages of using an extended class of AsyncTask , which contains only one or several methods.

Example:

  • I create a AsyncTask for every background rendering that I will use.

or

  • I create a AsyncTask for all processes, and within it I create several methods, and call them with a parameter when instantiating the class ?

Is it something to be considered, or just about organization, need, preference, or even good practice?

    
asked by anonymous 28.02.2018 / 20:53

1 answer

2

Not only in this case but whatever the situation may / should consider other approaches.

However, you should take into account not the organization, need, preference, or even good practices but what is most appropriate for the situation in question.

You should start by considering using an AsyncTask for each processing. If, during development, you verify that there are processing with similar characteristics you may consider writing a single class to process them.

Not in the way you describe your second example, which, if I understood correctly, will need to implement some logic to handle the different processing. This will not only complicate the class but complicate its use.

So I said in "processing with similar characteristics". For example, if you have a set of renderings that do not return anything and require a ProgressBar to appear, you can write an extended AsyncTask class where you implement all methods except doInBackground() .

When you have such a processing, you just need to implement this method:

new GenericAsyncTask() {
    @Override
    protected Void doInBackground(Void... voids) {

        //inserir aqui o processamento a executar
        return null;
    }
}.execute();

This is just an example, others will depend on the type of rendering to process.

    
28.02.2018 / 22:54