简体   繁体   中英

Progress dialog not showing while loading pictures

I have to load several pictures into the main thread and I want to show a loading dialog while this is happening, but somehow I can't get to show the progress dialog...

My code below:

private ProgressDialog progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.background);

    progress = ProgressDialog.show(this, "",getString(R.string.background_loading), true);

    LoadBackgrounds loadB = new LoadBackgrounds();
    new Thread(loadB).start();

}

public class LoadBackgrounds implements Runnable {
     @Override
     public void run() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                loadPictures();
                progress.dismiss();
            }
        });
     }
 }

loadPictures() must be called inside runOnUiThread because it modifies the views. So, why isn't the progress dialog showing while the pictures are loading?

I am not 100% sure but here is my guess:

  1. You create a Thread that should run separately from the UIThread but inside that you call runOnUiThread() which merges this thread with the UIThread.
  2. You create a runnable to run in a thread that creates a runnable that runs on another thread. I guess you should understand the nonsense of that
  3. Your loadPictures() will probably slow down the UIThread which could be the reason that you do not see the progress dialog in the first place and as soon as the loading is done you dismiss it.
  4. I also guess that you get either an ANR Dialog (Application not responding) or you see a lot of Skipped X frames! The application may be doing too much work on its main thread. Skipped X frames! The application may be doing too much work on its main thread. messages in your logcat output.

My advice and depending on what your loadPictures() method actually does: Use an AsyncTask or, when you access a server to get the pictures, check libraries like Volley or RetroFit and use them to get the data.

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