ProgressDialog considered harmful
Posted by David Chandler on March 26, 2013
Roman Nurik recently wrote a G+ post favoring the use of inline ProgressBar over the pop-up ProgressDialog. Here are some additional reasons to consider.
To be sure, ProgressDialog is very convenient. It has a nice spinner and does its job very simply. You can create one as simply as
ProgressDialog pd = ProgressDialog.show(this, "Please wait", "working..."); ... pd.cancel();
The first problem is that by default, there is no way for the user to dismiss a ProgressDialog. A common scenario is to show the ProgressDialog before making a network request. But what happens if the user gives up before the request times out? The back button doesn’t work. The home button works as usual, but if you return to the app, the ProgressDialog is still there. To get rid of it, you have to force close the app.
This is easily remedied by setting an additional property:
ProgressDialog pd = ProgressDialog.show(this, "Please wait", "working..."); pd.setCancelable(true); ... pd.cancel();
However, there is another problem which leads to a lot of confusion for Android or Java newbies. Consider this code which I have inadvertently written myself:
ProgressDialog pd = new ProgressDialog(this); pd.setCancelable(true); pd.show(this, "Please wait", "working...");
Like the first example, this will show a ProgressDialog which cannot be dismissed with the back button. Why? Because the show() method used here is a static method which returns a new ProgressDialog. The instance named pd is never shown. The compiler will show a warning that a static method is being invoked in a non-static way so you might get a clue that something is wrong; still, this is a really confusing API with no documentation. If you want to use the ProgressDialog constructor directly (as you must in order to subclass it), you must do this instead:
ProgressDialog pd = new ProgressDialog(this); pd.setTitle("Please wait"); pd.setMessage("working..."); pd.setCancelable(true); pd.show();
Note that the no-arg show() method used here is an instance method inherited from the Dialog class. The resulting ProgressDialog may indeed be canceled.
StackOverflow indicates that ProgressDialog has been a headache for more than a few developers.
So, like Roman said, use ProgressBar instead. Or if you don’t, make sure you’re using ProgressDialog in such a way that it can in fact be dismissed.