Reputation: 2312
I am creating a custom AlertDialog
to show loading on button click event listener. The Alert dialog show()
function works fine but the dismiss()
function is not working
public AlertDialog LoadDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
LayoutInflater inflater = activity.getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_show_loading, null));
builder.setCancelable(true);
dialog = builder.create();
if (dialog != null)
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
return dialog;
}
To show loading on button click on different class, I just call:
new LoadingDialog(context).LoadDialog().show(); //works fine
and dismiss loading I call:
new LoadingDialog(context).LoadDialog().dismiss(); // does not work
Upvotes: 1
Views: 481
Reputation: 36423
You should store the returned dialog into a variable otherwise all you are doing is creating a new AlertDialog
instance and calling show()
and then another new instance and calling dismiss()
(hence the one never dissappears):
AlertDialog dialog = new LoadingDialog(context).LoadDialog();
Then you can call:
dialog.show();
Or
dialog.dismiss();
Upvotes: 3
Reputation: 1107
You are doing new LoadingDialog(context).LoadDialog().dismiss(); // does not work
this will create a new dialog box reference and tries to dismiss it instead of using the same dialogue you have created before.
You need to store the created dialogue with this one -
new LoadingDialog(context).LoadDialog().show(); //works fine
and then use .dismiss
on that stored variable
Upvotes: 0
Reputation: 56697
Of course you‘re calling dismiss
on a different instance than the one created by the first call to show
, as the LoadingDialog
method creates a new instance on every call.
You need to store the instance created by show
and call dismiss
on that instance.
Upvotes: 0
Reputation: 1
There is no dismiss function AlertDialog.Builder Class. Instead use,
AlertDialog dialog= new AlertDialog.Builder(this).create();
and call
dialog.dismiss();
Upvotes: 0