SHASHIDHAR MANCHUKONDA
SHASHIDHAR MANCHUKONDA

Reputation: 3322

AsyncTasks in Main activty How to show the progress dailogue

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.

Upvotes: 2

Views: 175

Answers (5)

imthegiga
imthegiga

Reputation: 1126

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.

Upvotes: 2

Sanket Shah
Sanket Shah

Reputation: 4382

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.

Upvotes: 0

Exceptional
Exceptional

Reputation: 3004

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();

Upvotes: 0

Sushil
Sushil

Reputation: 8488

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);
        }
}

Upvotes: 1

Sumit Dhaniya
Sumit Dhaniya

Reputation: 596

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.

Upvotes: 0

Related Questions