简体   繁体   中英

Android Progress Dialog not showing until the function finish running (AsyncTask)

When a button is clicked I'm calling the async class in a function and I need to show progressDialog until it runs the displaylist function. But it shows up only after the function finished running and closes immediately. Please help me what am I doing wrong here.

public class FilterAsyncTask extends AsyncTask<Void, Void, Void> {

    ProgressDialog dispProgress;

     @Override
        protected void onPreExecute()
        {
            dispProgress = ProgressDialog.show(Filter.this, "Please wait...",
                    "Loading...", true, true);
        }

       protected Void doInBackground(Void... params) {

           return null;
       }

       protected void onPostExecute(Void result) {
          super.onPostExecute(result);

        MerchantsActivity.displayList();
            dispProgress.cancel();
            finish();
       }

    }

Your AsyncTask will complete immediately because you do exactly nothing in doInBackground()! That's where your long-running background non-UI code is supposed to go...

I would recommend you not to use the static ProgressDialog#show method. Rather do new ProgressDialog() and initialize it accordingly and finally call show() . I have never used the static method and do not know how it works, but I have used the other option. Furthermore the static method seems to have no available documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM