在我們看來兩者效果都是一樣的,其實看下源碼就知道cancel肯定會去調dismiss的,如果調用的cancel的話就可以監聽DialogInterface.OnCancelListener。
/** * Cancel the dialog. This is essentially the same as calling {@link #dismiss()}, but it will * also call your {@link DialogInterface.OnCancelListener} (if registered). */ public void cancel() { if (!mCanceled && mCancelMessage != null) { mCanceled = true; // Obtain a new message so this dialog can be re-used Message.obtain(mCancelMessage).sendToTarget(); } dismiss(); }
dismiss可以在任何線程調用,但是最好不要覆寫dismiss方法,實在需要就在onStop里去override。
/** * Dismiss this dialog, removing it from the screen. This method can be * invoked safely from any thread. Note that you should not override this * method to do cleanup when the dialog is dismissed, instead implement * that in {@link #onStop}. */ @Override public void dismiss() { if (Looper.myLooper() == mHandler.getLooper()) { dismissDialog(); } else { mHandler.post(mDismissAction); } }
在dismissDialog里調用了onStop
void dismissDialog() { if (mDecor == null || !mShowing) { return; } if (mWindow.isDestroyed()) { Log.e(TAG, "Tried to dismissDialog() but the Dialog's window was already destroyed!"); return; } try { mWindowManager.removeViewImmediate(mDecor); } finally { if (mActionMode != null) { mActionMode.finish(); } mDecor = null; mWindow.closeAllPanels(); onStop(); mShowing = false; sendDismissMessage(); } }
補上hide方法,注釋上說了hide只是隱藏了對話框並沒有銷毀,如果打算用這方法來滅掉對話框就會出現問題,在Activity銷毀的時候就會出現崩潰日志了,因為
Activity銷毀時是需要把對話框都關閉掉的。
日志如下:
android.view.WindowLeaked: Activity com.example.androidtest_progressdialog.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{434557e8 G.E..... R.....ID 0,0-1026,288} that was originally added here
再看一下hide里是做什么操作:
/** * Hide the dialog, but do not dismiss it. */ public void hide() { if (mDecor != null) { mDecor.setVisibility(View.GONE); } }
可以看出,hide只是把對話框里的DecorView設置為不可見,並沒有從Window移除掉這個DecorView。