简体   繁体   中英

Best practice to show ProgressDialogs through an application?

In my application I'd like to show a ProgressDialog on certain events (during AsyncTasks for example). I never want to show multiple ProgressDialogs simultaneously.

What is the best and most efficient way to do this? My current code looks like this:

public class ProgressDialogHelper {
    private static ProgressDialog progressDialog;

    public static void showProgressDialog(Context c) {
        if (progressDialog == null) {
            progressDialog = ProgressDialog.show(c, null, null);
            progressDialog.setContentView(R.layout.progress_bar);
        }
    }

    public static void dismissProgressDialog() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
}

I'm calling these methods from my other classes to show/dismiss the ProgressDialog .

Is this considered good practice? If not, how should I do this?

You are asking for the best practice. In fact the best practice is to not use ProgressDialog s, because they block the user from interacting with the app.

There are many alternatives to not use a ProgressDialog .

If your app is loading data, which is required for a list, place a Spinner instead of the ListView.

For some additional reading in this topic see here: Google Design Guidelines: Progress


If you still want to use a ProgressDialog i would highly recommend to not use the class you posted above. It holds a static reference to a progressDialog and so it's global above all activities and live cycles. If your application is recreated or something else this can cause some trouble.

I would recommend an non static ProgressDialogHelper you can initialize for every Activity and this one will then handle the Dialog . Also make sure to handle livecycle methods like onDestroy


Having a BaseActivity as @jagmohan noted is possible, but if you don't want to have your own BaseActivity and want to use something as drop in. You can create something like this:

ProgressDialogHelper.java

public class ProgressDialogHelper {
    ProgressDialog mProgressDialog;

    public ProgressDialogHelper() {

    }

    public ProgressDialogHelper(Context context, String title, String message) {
        mProgressDialog = new ProgressDialog(context);
        mProgressDialog.setTitle(title);
        mProgressDialog.setMessage(message);
        mProgressDialog.show();
    }

    public ProgressDialog getProgressDialog() {
        return mProgressDialog;
    }

    public void setProgressDialog(ProgressDialog mProgressDialog) {
        //make sure the previous dialog is hidden
        hide();
        this.mProgressDialog = mProgressDialog;
    }

    public void show() {
        if (mProgressDialog != null && !mProgressDialog.isShowing()) {
            mProgressDialog.show();
        }
    }

    public void create(Context context, String title, String message) {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
        mProgressDialog = ProgressDialog.show(context, title, message);
    }

    public void hide() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    public void onDestroy() {
        hide();
    }
}

As pointed out by @mikepenz, using ProgressDialogs is discouraged in the latest design guidelines. However, here is something which worked well for me in the past.

I used to setup a base Activity and add couple of methods to hide and show progress dialog in that class.

public class BaseActivity extends ActionBarActivity {

    protected static final String TAG = BaseActivity.class.getSimpleName();
    protected ProgressDialog mProgressDialog;

    protected void showProgressDialog(String title, String message) {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
        mProgressDialog = ProgressDialog.show(this, title, message);
    }

    protected void hideProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        // hide progress dialog to prevent leaks
        hideProgressDialog();
    }

    // other stuff...
}

Now, you can extend this base activity in the new activity classes you create in your project and use these methods to show/hide progress dialogs.

Btw, just make sure that you call show/hide methods from the UI thread.

Hope this answers your question.

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