如何从自定义对话框类关闭活动?
我有一个 AlertDialogUtils
类,它生成一个 AlertDialog
,以便在发生错误时可以从任何活动调用它。问题是我无法从 createDialog()
方法中调用 finish()
作为“关闭”按钮的 onClickListener
。
有什么想法这可能吗?
AlertDialogUtils 类的代码:
public class AlertDialogUtils extends Dialog {
private Context mContext;
public AlertDialogUtils(Context context) {
super(context);
mContext = context;
}
public void CreateAlertDialog(String errorMessage) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(errorMessage)
.setCancelable(true)
.setNeutralButton("Dismiss", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mContext.finish();
//error here. Intend to close the activtiy that created this dialog and has the error
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity((Activity)mContext);
// The dialog utils is outside an activity. Need to set owner
alert.show();
}
}
I have an AlertDialogUtils
class that generates an AlertDialog
such that it can be called from any activity when an error occurs. The issue is that I cannot call finish()
from within the createDialog()
method as a onClickListener
for a "dismiss" button.
Any thoughts how this may be possible?
Code for AlertDialogUtils class:
public class AlertDialogUtils extends Dialog {
private Context mContext;
public AlertDialogUtils(Context context) {
super(context);
mContext = context;
}
public void CreateAlertDialog(String errorMessage) {
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setMessage(errorMessage)
.setCancelable(true)
.setNeutralButton("Dismiss", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mContext.finish();
//error here. Intend to close the activtiy that created this dialog and has the error
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity((Activity)mContext);
// The dialog utils is outside an activity. Need to set owner
alert.show();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可以请你尝试一下这个方法吗? :
Could you please try it this way please? :
不要在构造函数中传递
Context
,而是传递Activity
。它继承了Context
,因此您可以在任何需要Context
的地方使用它;同时,您也可以在需要时调用finish()
。Instead of passing a
Context
in the constructor, pass anActivity
. It inheritsContext
, so you can use it anywhere you need aContext
; at the same time, you can also callfinish()
when you need it.感谢您的建议。不幸的是,
ICloseActivity
对我来说不起作用。我通过以下方式解决了完成活动并关闭对话框的问题:我按如下方式更改了
CreateAlertDialog()
方法,并创建了一个辅助函数exitActivity()
:在调用此实用程序时,我只需设置关闭侦听器并完成活动,以防万一被解雇了。
通过这种方式,如果需要,我可以处理关闭当前活动。
另外,对于我不想关闭而不是调用 exitActivity() 的活动,我调用了dialog.cancel():
Thanks for the suggestions.
ICloseActivity
, unfortunately, did not work for me. I resolved the problem of finishing the activity and dismissing the dialog box in the following manner:I changed the
CreateAlertDialog()
method as follows and created a helper functionexitActivity()
:While calling this utility, I simply set the dismiss listener and finish the activity incase it is dismissed.
In this way I can handle closing the current acitivity if required.
Also, for activities that I didn't want to close instead of calling
exitActivity()
, I calleddialog.cancel()
: