Android - What is difference between progressDialog.show() and ProgressDialog.show()?

血红的双手。 提交于 2019-12-12 02:46:36

问题


I mean, what is difference between return value of ProgressDialog static method show() and the non-static method show of an instance of that class?

Is there any reason to prefer this strategy

ProgressDialog pd = new ProgressDialog(mActivity);
pd.setTitle(mTitle);
pd.setMessage(mMessage);
pd.show();

to this:

ProgressDialog pd = ProgressDialog.show(mActivity,mTitle,mMessage);

for a particular situation?


回答1:


In my opinion, the "correct" method would depend on your usage. The static show( ... ) methods are performing the same steps you are:

public static ProgressDialog show(Context context, CharSequence title,
        CharSequence message) {
    return show(context, title, message, false);
}

public static ProgressDialog show(Context context, CharSequence title,
        CharSequence message, boolean indeterminate) {
    return show(context, title, message, indeterminate, false, null);
}

public static ProgressDialog show(Context context, CharSequence title,
        CharSequence message, boolean indeterminate, boolean cancelable) {
    return show(context, title, message, indeterminate, cancelable, null);
}

public static ProgressDialog show(Context context, CharSequence title,
        CharSequence message, boolean indeterminate,
        boolean cancelable, OnCancelListener cancelListener) {
    ProgressDialog dialog = new ProgressDialog(context);
    dialog.setTitle(title);
    dialog.setMessage(message);
    dialog.setIndeterminate(indeterminate);
    dialog.setCancelable(cancelable);
    dialog.setOnCancelListener(cancelListener);
    dialog.show();
    return dialog;
}

You can see that any calls to the static show methods with parameters just ends up constructing a ProgressDialog and will call the instance method show().

Using the static show( ... ) methods just make it convenient for you to display a basic ProgressDialog using one line of code.




回答2:


writing it with capital p is the correct way to go since the method show is static

ProgressDialog.show(mActivity,mTitle,mMessage);

see the doc here

Is there any reason to prefer this strategy??

the reason why is the best way to go is that static methods should always be accessed in a static way



来源:https://stackoverflow.com/questions/40748617/android-what-is-difference-between-progressdialog-show-and-progressdialog-sh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!