开发者

Should I check if ProgressBar isShowing before dismissing it?

开发者 https://www.devze.com 2023-04-13 07:16 出处:网络
Im dismissing progress dialog when AsyncTask is finished. Should i check isShowing before dismissing it?

Im dismissing progress dialog when AsyncTask is finished. Should i check isShowing before dismissing it?

I've tried remove this check and it works normally, but may be there are h开发者_如何学JAVAidden traps?

if (progressDialog.isShowing()) {
  progressDialog.dismiss();
}


You shouldnt have to check for isShowing to dismiss it. If you dont check for isShowing it will just ignore the dismiss() is the progressbar isnt showing.

But checking for isShowing is a good practice. So it wont hurt to continue to check for it.


Is seems that is checked inside implementation:

public void dismiss() {
    if (Thread.currentThread() != mUiThread) {
        mHandler.post(mDismissAction);
    } else {
        mDismissAction.run();
    }
}

private void dismissDialog() {
    if (mDecor == null || !mShowing) {
        return;
    }

    try {
        mWindowManager.removeView(mDecor);
    } finally {
        mDecor = null;
        mWindow.closeAllPanels();
        onStop();
        mShowing = false;

        sendDismissMessage();
    }
} 


If you only have one place in your code that where dismiss() is called, then no. You should know if the dialog is showing or not at that time.

I have however experienced some problems with ProgressDialogs and orientation change, which caused the need for have more than one place where the dialog potentially would be dismissed, thus making the isShowing() check needed.


check the source code, if it is handled in code you don't have to worry. If no handling is done it is your choice to create more-risky-but-fast or slow-but-safe environment.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号