私はAlertDialog
を生成するAlertDialogUtils
クラスを持っており、エラーが発生したときにどのアクティビティからでも呼び出すことができます。問題は、私がcreateDialog()
メソッド内からfinish()
をonClickListener
と "dismiss"ボタンとして呼び出すことができないことです。カスタムダイアログボックスからアクティビティを閉じるには?
これはどのように考えられますか?
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();
}
}
mContext.finish(): はまた、私は
exitActivity()
を呼び出す代わりに、クローズしたくなかった活動のために、私はdialog.cancel()
と呼ばれますか? –おそらく、これは最も直接的な方法ではありませんが、アクティビティをパラメータとして 'CreateAlertDialog(String)'に渡さないのはなぜですか? – Brian