I have 3 async tasks in my Mainactivty Oncreate() Method when user opens the app i want to show the Progress dialogue while the 3 asynctasks are loaded. please specify where do i start and dismiss the progess dialogue. if any sample please provide a link . Thank you.
-
Is there any code you can provide to us? – ksugiarto Aug 21 '13 at 03:50
-
What you have done so far?You can start it within pre execute of first asyncTask & dismiss it within post execute of last AsyncTask. – Bhoomika Brahmbhatt Aug 21 '13 at 03:50
-
I think indeterminate progress bar will be the best solution in this case. – Milan Aug 21 '13 at 03:51
-
You can use progress bar and update the progress bar inside `onProgressUpdate` method. Cheers – user2652394 Aug 21 '13 at 03:53
-
why 3 asynctask not one? – TheLittleNaruto Aug 21 '13 at 04:38
-
go with one async task.display progress dialog in preexecute method. – hemanth kumar Aug 21 '13 at 04:53
-
Using several asynctask is not a good idea – An-droid Sep 27 '13 at 09:27
5 Answers
Actually you got the code how to do it. In addition I will suggest that, you have to create and show a progress dialog before any call to AsyncTask
. As you've 3 AsyncTasks, you simply can't say which is going to be a last task if you are fetching something from server. If you have such case then make 3 flags like boolean task1, task2, task3;
and in onPostExecute
method of each task, make respective flag to true and check whether these all flags are true or not like if(task1 && task2 && task3) progressDialog.dismiss();
. So for last task it will set it's flag to true and 2 flags will be already set to true, hence as condition is true it will dismiss progress dialog. Hope you got it.

- 1,126
- 8
- 13
-
thank you for your answer i have same thing in mind i am checking for other solutions to do it.. – SHASHIDHAR MANCHUKONDA Aug 21 '13 at 07:27
You can declare progress bar. Then in onPreExecute, show progressbar. In doInBackground you can calculate progress value and call publishProgress to show progress value. In onPostExecute you can dismiss progressbar.
You can do something like this:
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading data file..");
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
private class DownloadZipFileTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... urls) {
Long progress;
calculate progress
publishProgress("" + progress);
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String result) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}

- 8,250
- 3
- 39
- 71
-
Hi.. in case this answer helped you to solve your issue, please do accept the answer so that others can get to know the correct answer and the thread becomes more useful. – Sushil Aug 21 '13 at 05:24
You can start the progress dialog in the onPreExecute method of your 1st AsyncTask and when onPostExecute is called for any AsyncTask you can check if other AsyncTask are finished , if yes dismiss the dialog otherwise let it run. Alternatively you can start a separate progress dialog for each AsyncTask and dismiss them upon the finishing of respective AsyncTask.

- 596
- 6
- 14
It's better to start the progress bar in the first async class and using the same object dismiss the progress bar in the last async class.... Leave the rest.
obj.dismiss();

- 2,994
- 1
- 18
- 25
Here is your ayncTask and call it with new Async(Your_Context).execute();
private class Async extends AsyncTask<String, String, Boolean>
{
Context context;
ProgressDialog pd;
Progress progress;
Async (Context mcontext)
{
context = mcontext;
pd = new ProgressDialog(activityContext);
progress = new Progress(context);
}
@Override
protected void onPreExecute()
{
pd.setTitle("Loading..");
pd.setMessage("Please Wait..");
pd.setCancelable(false);
pd.show();
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... params)
{
return flag;
}
@Override
protected void onProgressUpdate(String... values)
{
super.onProgressUpdate(values);
pd.setMessage("Please Wait..." + values[0].toString());
}
public class Progress
{
public ServerToApp task;
public Progress(ServerToApp task)
{
this.task = task;
}
public void publish(String value)
{
task.publishProgress(value);
}
}
@Override
protected void onPostExecute(Boolean result)
{
super.onPostExecute(result);
if(pd.isShowing()) pd.dismiss();
}
}
And for updating the progress you should pass thw values like this from your method like this
progress.publish(Values);
Hope it will help you.

- 4,352
- 3
- 21
- 41