How to run two AsyncTasks in one action?

I have two Activity ( mainActivity and downloadActivity ), and I have 2 AsyncTask in downloadActivity

In downloadActivity , it first executes getFileAsyncTask to read the JSON file to add some images and create a ListView from the images, if the user clicks on the image, downloadAsyncTask called, and it starts downloading something from the Internet. My problem is here: when the second AsyncTask , I return to mainActivity and again return to downloadActivity , the first AsyncTask not called until downloadAsyncTask completed

 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 an BaseAdabter 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 } } 

note: I want to write something like trading applications. For example, a user can upload files and shop at a store to see products.

+6
source share
4 answers

If you want to run several AsyncTasks in parallel, you can call executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR) instead of execute() in your task. By default, AsyncTasks starts in sequential order, you must first submit.

Be careful that the two threads do not interact on the same data, this can cause some strange and difficult error traces.

+13
source

you can do this in one asynk class that has two methods, first get the answer to wait for the json file in doInBackground ... if this is normal, enter the file upload code. These methods will return an httpResponse object httpResponse

0
source

make one class object, for example:

declare a progress bar globally in the main thread.

now what you need to do is run one async task in the main thread, like

  public class myactivityextends 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(); } } } } } 
0
source

You can override the onBackPressed function in action and end the current activity before moving on to the previous operation. When you come to downloadActivity again, it will call its oncreate method and call AsynctTask first.

0
source

Source: https://habr.com/ru/post/969957/


All Articles