Progress dialog UI Freezes/Slow

倾然丶 夕夏残阳落幕 提交于 2019-12-04 02:59:46

This probably happens because your background thread consumes 100% of device CPU. When CPU is busy processing one thread, UI thread won't be updated and therefore you see it as frozen

Try to detect what's the most aggressive operation by removing parts of code from your doInBackground and running the app again. Also try to see how it performs when device is not plugged via USB - this some times provides weird results

Figured it out, I had a runaway method going on the UI thread that i did not notice

You are using the ProgressDialog wrong.

You need to add a onPreExecute method, and there you define and show your ProgressDialog. Then doInBackground is performed on another thread, and eventually in onPostExecute you dismiss the dialog.

Here's a simple example:

class RefreshChanges extends AsyncTask<String, Void, String> {
        private ProgressDialog mProgressDialog = new ProgressDialog(
                mContext);

        @Override
        protected void onPreExecute() {
            mProgressDialog.setTitle("Whatever title");
            mProgressDialog.setMessage("Whatever message");
            mProgressDialog.show();
        }

        protected String doInBackground(String... strings) {
            // Do whatever processing you want...
            return "";
        }

        protected void onPostExecute(String result) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }
    new RefreshChanges().execute();

By the way, I also recommend you not to use hardcoded strings. Instead, you can go to the strings.xml file under res\values\ and define a string. Then in your code, you can use either getString(R.string.yourStringId) or R.string.yourStringId. It depends whether the method accepts Id's or not (Methods that accept Id's, actually perform getString with the Id you sent it).

i don't see any fault in your code, but you have to understand that only writing code in another thread does not means it will get another processor. if your device is having single core processor it does time slicing and does work in round robin way. if your device has multiple cores it will do actual multiple threading. so if you have single core processor it will show you some lag in progress bar.

CAM-Dev

I have implemented the dialog this way with success.

private ProgressDialog progress;

private class AsynTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
           progress = ProgressDialog.show(context, "", "Please wait...", true);

       }

        @Override
        protected void onPostExecute(Void params) {
             if (progress.isShowing()) 
                  progress.dismiss();

       }

        @Override
        protected Void doInBackground(Void... arg0) {
             // Do some work
             return null;
       }
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!