The overall picture is started by AsyncTask in the onPreExecute method, preparation is performed. Then in doInBackground , inside which sequentially runs thread by thread in newSingleThreadExecutor() and when all threads run in newSingleThreadExecutor() then doInBackground finishes its execution, the last onPostExecute method is onPostExecute in which I write to the user that everything went well.
The question is, will doInBackground really wait for newSingleThreadExecutor() inside it to execute all its threads?
Here is the code how it works for me
new AsyncTask<Void, Void, Void>() { MyDialog dialog; @Override protected void onPreExecute() { super.onPreExecute(); dialog = MyDialog.newInstance(R.layout.activity_good_job_dialog); dialog.show(getFragmentManager(), "dialog"); } @Override protected Void doInBackground(Void... params) { ExecutorService pool = Executors.newSingleThreadExecutor(); pool.submit(new ZipUtil()); pool.submit(new Runnable() { @Override public void run() { sentPhoto(); } }); pool.submit(new Runnable() { @Override public void run() { closeCamera(); stopBackgroundThread(); dialog.dismiss(); } }); pool.shutdown(); return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); startActivity(new Intent(getApplicationContext(), SuccessActivity.class) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK)); } }.execute();