Reputation: 311
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?
Upvotes: 1
Views: 566
Reputation: 57682
I am not 100% sure but here is my guess:
runOnUiThread()
which merges this thread with the UIThread.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.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.
Upvotes: 5