I have these 2 methods to create and destroy AlertDialog which only has progress bar.
public static AlertDialog.Builder showProgressAlertDialog(Context context, String title){
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(context, R.style.AlertDialogTheme);
builder.setTitle(title);
ProgressBar progressBar = new ProgressBar(context);
builder.setView(progressBar);
builder.show();
return builder;
}
public static void dismissProgressAlertDialog(AlertDialog.Builder builder){
builder.show().cancel();
}
There are no errors while creating it. However, when I call the dismiss method, application throws the following exception: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Note that, I need to call this method and destroy the dialog from another class, therefore onClick method will not work for me.
Use Dismiss() on the instance variable of AlertDialog
private AlertDialog dialog;
onCreate() {
dialog = createAlertDialog(context, title);
}
public static AlertDialog createAlertDialog(Context context, String title){
final AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.AlertDialogTheme);
builder.setTitle(title);
ProgressBar progressBar = new ProgressBar(context);
builder.setView(progressBar);
return builder.create();
}
public static void dismissAlertDialog() {
dialog.dismiss();
}