SoFunction
Updated on 2025-03-11

Prevent AlertDialog from closing instance code in Android

How to close the dialog box on Android? There is no such method as finish() or dismiss() in the dialog box.

However, its parent class AlertDialog has a dismiss method, and when .show(), it will get an AlertDialog object, so we can use the dismiss method to close the Builder.

 builder = new (this); 
AlertDialog dialog = (); 
(); 

The specific code for Android preventing AlertDialog from closing is as follows:

 builder = new (getActivity());
("test");
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = (.dialogfragment_num_input, null);
(view);
("Sure",
new () {
@Override
public void onClick(DialogInterface dialog, int id) {
Field field = null;
try {
//Retrieve private attributes in the dialog through reflection mShowingfield = ().getSuperclass().getDeclaredField("mShowing");
(true);//Set this property to access} catch (Exception ex) {
}
String inputValue = (());
if (inputValue == null || "".equals(inputValue)) {
try {
//Set dialog cannot be closed(dialog, false);
();
} catch (Exception ex) {
}
} else {
//
//Do your own thing//
try {
//closure(dialog, true);
();
} catch (Exception ex) {
}
}
}
});
("Cancel",
new () {
@Override
public void onClick(DialogInterface dialog, int id) {
Field field = null;
try {
//Retrieve private attributes in the dialog through reflection mShowingfield = ().getSuperclass().getDeclaredField("mShowing");
(true);//Set this property to access} catch (Exception ex) {
}
try {
(dialog, true);
();
} catch (Exception ex) {
}
}
});
();