我什么时候应该关闭对话框?
我已经阅读了一些有关 Android 中对话框的内容,并且有一个未解决的问题:
当我使用处理对话框的 Activity 方法(例如:onCreateDialog(...)
)时,我应该或者我不应该在 onPause()
中关闭对话框吗? 或者也许我应该仅在保留它时才将其关闭 - 创建一个引用此对话框的 Activity 成员变量?
我找到了这个答案: https://stackoverflow.com/a/2851833/501560 说我需要明确调用 dismiss()
方法,但我读过一些其他资源,说 Activity 应该自行处理它......
谢谢。
I've done some reading about dialogs in Android and I have an open issue:
When I'm using the Activity's methods that handle the dialogs (such as: onCreateDialog(...)
), should I or shouldn't I dismiss the dialog in the onPause()
?
Or maybe I should dismiss it only if I retained it - made an Activity member variable that has a reference to this dialog?
I've found this answer: https://stackoverflow.com/a/2851833/501560 saying that I need to explicitly call the dismiss()
method, but I've read some other resources saying that the Activity should handle it by itself...
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
关闭对话框
当您准备好关闭对话框时,可以通过调用 Dialog 对象上的 dismiss() 来关闭它。如有必要,您还可以从 Activity 调用 DismissDialog(int),这会有效地为您调用 Dialog 上的 Dismiss()。
如果您使用 onCreateDialog(int) 来管理对话框的状态(如上一节所述),则每次关闭对话框时,Activity 都会保留 Dialog 对象的状态。如果您决定不再需要此对象或者清除状态很重要,那么您应该调用
removeDialog(int)
。这将删除对该对象的任何内部引用,如果该对话框正在显示,它将关闭它。使用关闭侦听器
如果您希望应用程序在对话框关闭时执行某些过程,那么您应该将关闭侦听器附加到您的对话框。
首先定义
DialogInterface.OnDismissListener 接口
。该接口只有一个方法,onDismiss(DialogInterface)
,当对话框关闭时将调用该方法。然后只需将您的OnDismissListener
实现传递给“但是,请注意,对话框也可以被“取消”。这是一种特殊情况,表明该对话框已被用户明确取消。如果用户按“后退”按钮关闭对话框,或者对话框显式调用
cancel()
(可能是通过对话框中的“Cancel
”按钮),就会发生这种情况。对话)。当对话框被取消时,OnDismissListener 仍然会收到通知,但是如果您想被告知对话框已被显式取消(并且通常不会被关闭),那么您应该注册一个Dismissing a Dialog
When you're ready to close your dialog, you can dismiss it by calling dismiss() on the Dialog object. If necessary, you can also call dismissDialog(int) from the Activity, which effectively calls dismiss() on the Dialog for you.
If you are using
onCreateDialog(int)
to manage the state of your dialogs (as discussed in the previous section), then every time your dialog is dismissed, the state of the Dialog object is retained by the Activity. If you decide that you will no longer need this object or it's important that the state is cleared, then you should callremoveDialog(int)
. This will remove any internal references to the object and if the dialog is showing, it will dismiss it.Using dismiss listeners
If you'd like your application to perform some procedures the moment that a dialog is dismissed, then you should attach an on-dismiss listener to your Dialog.
First define the
DialogInterface.OnDismissListener interface
. This interface has just one method,onDismiss(DialogInterface)
, which will be called when the dialog is dismissed. Then simply pass yourOnDismissListener
implementation toHowever, note that dialogs can also be "cancelled." This is a special case that indicates the dialog was explicitly cancelled by the user. This will occur if the user presses the "back" button to close the dialog, or if the dialog explicitly calls
cancel()
(perhaps from a "Cancel
" button in the dialog). When a dialog is cancelled, the OnDismissListener will still be notified, but if you'd like to be informed that the dialog was explicitly cancelled (and not dismissed normally), then you should register an如果对话框由活动管理,则您永远不必关闭该对话框。
当对话框被销毁时,活动将关闭该对话框。如果活动暂停,则不必关闭对话框。
You never have to dismiss the dialog if it's managed by the Activity.
The Activity will dismiss the dialog when it's destroyed. If the Activity is pause, Dialog doesn't have to be dismissed.
我认为对话框应该在 Activity 生命周期的
onStop()
或onPause()
中关闭。https://developer.android.com/reference/android/ app/Dialog.html#dismiss()
I thought dialogs are supposed to be dismissed in
onStop()
oronPause()
in the Activity lifecycle.https://developer.android.com/reference/android/app/Dialog.html#dismiss()