I make an application on Android. I try to make a call to run the Asynctask thread in a Asynctask , but the problem is that when the loop is executed, the thread does not stop and the code continues to run before the thread is completed.
For example:
for (int i =0; i<array.size; i++) { ......(код) new MyAsynctask().execute(); ......(код) } Intent intent = new Intent(this, Activity2.class); startActivity(intent); where Asynctask itself:
class MyAsynctask extends AsyncTask<Void, Void, Void> { @Override protected void onPreExecute() { super.onPreExecute(); pDialog = new ProgressDialog(MainActivity.this); pDialog.setMessage("Loading!!!"); pDialog.setCancelable(false); pDialog.show(); } @Override protected Void doInBackground(Void... arg0) { ........... (вся тяжелая работа) } } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); if (pDialog.isShowing()) pDialog.dismiss(); } With this code, the async is called in a loop, it starts to execute and immediately after it, the code in the UI starts to be executed even if the asynccusc has not finished its work. I want the asinkt to be executed to the end, then the subsequent code will be executed sequentially and after going through (completing) the whole cycle, a transition to another activation was performed. This can be done with the get () method.
new MyAsynctask().execute().get(); everything seems to work this way, but the problem is that the UI stream stops then until the Asynctask thread ends. That is, no action takes place on the display (I need the Progress Bar to spin).
Write:
Intent intent = new Intent(this, Activity2.class); startActivity(intent); in onPostExecute(); also not suitable as Asynctask is called several times in a loop. And I need to call the Activity call only once after the end of the cycle.
Can there be an opportunity to manually make a progress bar when the get() method is executed? What other options are there? Thank!