Android 开发 对话框Dialog dismiss和hide方法的区别

2025-05-20 04:35:53
推荐回答(2个)
回答1:

dismiss和hide方法都可以隐藏对话框,在需要的时候也可以用show方法调用显示。但是,这两者是有区别的。
dismiss方法会释放对话框所占的资源,而hide方法不会。activity退出前必须调用dismiss方法关闭对话框。
如果对话框上有progressbar,你会发现,调用dismiss方法后,再调用show方法,出来的对话框,上面的progressbar不再会转动,而调用hide方法的则没有问题。
所以,最正确的调用方法是,在activity的onDestory方法里调用dismiss方法,其他地方都用hide方法隐藏对话框。

回答2:

/**
* Hide the dialog, but do not dismiss it.
*/
public void hide() {
   if (mDecor != null) {
       mDecor.setVisibility(View.GONE);
   }
}

/**
* 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);
   }
}

直接看源码,找区别