Saturday 2 July 2016

Progress dialog visibility in single asynctask android

ProgressDialog mProgressDialog;
public class getData extends AsyncTask<Void,Void,Void>
    {

        private boolean showLoading;

        public getDocumnet(boolean showLoading) {
            this.showLoading = showLoading;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            if (showLoading==true)
            {
                   mProgressDialog=ProgressDialog.show(MainActivity.this,"",getString(R.string.loading));
            }

        }

        @Override
        protected Void doInBackground(Void... voids) {

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            if (showLoading==true)
            {
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                }
            }

            
        }
    }

Call this asynctask like below

new getData(true).execute(); // if you want to show progress dialog

new getData(false).execute(); // if you want to hide progress dialog