¿Cómo ejecutar dos AsyncTasks en una actividad?

tengo dos Activity(mainActivity & downloadActivity) and I have 2 AsyncTasks en downloadActivity

In downloadActivity first it execute getFileAsyncTask for reading a JSON file for adding some images and create a ListView from images, if user clicks on an image, the downloadAsyncTask was called and it starts to download something from the internet. My problem is here: when the second AsyncTask is running I go back to mainActivity and comeback again to downloadActivity la primera AsyncTask wasn't called until the downloadAsyncTask completado.

public class downloadActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        new getFileAsyncTask().execute();
        ...
    }
    private class getFileAsyncTask extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
        //fetch a json file from internet and add images in ListView
            return null;
        }
    }
    //there is a Base Adapter class 
    //if user clicks on an image it calls this downloadAsyncTask.execute()
    private class downloadAsyncTask extends AsyncTask<Void, Integer, Void> {
        @Override
        protected void onPreExecute() {
        //download the file
        }
    }

    @Override
    protected Void doInBackground(Void... unused) {
        //download the file
    }
}

Nota: I want to write something like shopping apps. For example, user can download file and surf into shop to see products .

preguntado el 28 de mayo de 14 a las 13:05

4 Respuestas

If you want to run multiple AsyncTasks in parallel, you can call executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) en lugar de execute() on your task. By default, AsyncTasks run in serial, first come first serve.

Be careful that the two threads do not interact on the same data, this can cause some strange and hard to trace errors.

contestado el 28 de mayo de 14 a las 13:05

you can make it in one asynk class that have two methodes first fetch json file wait response in doInBackground ... if it is ok call download file methode. Those methodes will return an httpResponse objeto

contestado el 28 de mayo de 14 a las 13:05

You can override onBackPressed function in activity and finish the current activity before go to previous activity. When again you come to downloadActivity it call it's oncreate method and call first AsynctTask.

contestado el 28 de mayo de 14 a las 13:05

Make one class task like:

Declare progress bar globally in main thread.

Now what you have to do is start one async task in main thread like:

public class myactivity extends Activity {
    private ProgressDialog _dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_screen);
        new abc().execute();
    }

    class abc extends AsyncTask(String,String,String) {
        @Override
        protected void onPreExecute() {
            _dialog = new ProgressDialog(_ctx);
            _dialog.setCancelable(false);
            _dialog.setMessage("Loading");
            _dialog.show();
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {
            @Override
            protected void onPostExecute(String result) {
                // in post execute of this class you can run a new thread of your downaloder thread. and in post execute of last thread you have to dismiss the progess bar.
                new download activity.execute();
                }
            } 
        }
    }
}

Respondido 25 Abr '20, 13:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.