Reputation: 151
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();
}
}
Upvotes: 2
Views: 8521
Reputation: 921
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 function exitActivity()
:
public void CreateAlertDialog(String title, String errorMessage, int alertType){
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(title)
.setIcon(R.drawable.alert_icon)
.setMessage(errorMessage)
.setCancelable(false)
.setPositiveButton("Dismiss", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismiss = true;
exitActivity();
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity((Activity)mContext);
alert.show();
}
public void exitActivity() {
this.getOwnerActivity().finish();
}
While calling this utility, I simply set the dismiss listener and finish the activity incase it is dismissed.
dialog = new AlertDialogUtils(MyActivity.this);
dialog.setOwnerActivity(MyActivity.this);
dialog.CreateAlertDialog(getString(R.string.no_data),
getString(R.string.empty_table_message), R.id.list_view_alertType);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener(){
@Override
public void onDismiss(DialogInterface arg0) {
finish();
}
});
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 called dialog.cancel()
:
.setPositiveButton("Dismiss", new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
cancel();
}
});
Upvotes: 2
Reputation: 75991
Instead of passing a Context
in the constructor, pass an Activity
. It inherits Context
, so you can use it anywhere you need a Context
; at the same time, you can also call finish()
when you need it.
Upvotes: 2
Reputation: 2171
Could you please try it this way please? :
interface ICloseActivity {
void close();
}
class MyActivityToClose extends Activity implements ICloseActivity {
public void close() {
finish();
}
}
// -------
public class AlertDialogUtils extends Dialog {
private Context mContext;
private ICloseActivity mICloseActivity;
public AlertDialogUtils(Context context, ICloseActivity aActivity) {
super(context);
mContext = context;
mICloseActivity = aActivity;
}
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
//TRY THIS please:
mICloseActivity.close();
}
});
AlertDialog alert = builder.create();
alert.setOwnerActivity((Activity)mContext);
// The dialog utils is outside an activity. Need to set owner
alert.show();
}
Upvotes: 2