I want to create a Fragment Dialog such as BottomSheetDialog, AppCompactDialog, and so on. And when dismissing the dialog, I want to raise a question first. Where does the dialog proceed to dismiss or cancel. Therefore I want to utilize the OnCancelListener event as the path. However, when I tried it, the setOnCancelListener event function did not run or work properly. Why is that?
public class MyExample extends Activity implements DialogInterface.OnCancelListener {
@Override
public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
Dialog exDialog = new Dialog(this);
exDialog.setCancelable(false);
exDialog.setCanceledOnTouchOutside(false);
// ↓ THIS METHOD NOT WORKING, WHY??
exDialog.setOnCancelListener(this);
exDialog.setTitle("Example Dialog");
exDialog.show();
}
// ↓ EVENT NOT WORK AFTER TOUCH SCREEN DEVICE
@Override
public void onCancel(DialogInterface dialog) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Exit Application")
.setMessage("Are You Sure to Exit Application?")
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface subDialog, int id) {
// ↓ Dismiss ExampleDialog from cancel handling.
dialog.dismiss();
}
})
.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface subDialog, int id) {
// ↓ Not return to dismiss when this button clicked.
dialog.cancel();
}
}).show();
}
}
Problem Method setOnCancelListener not working.
exDialog.setOnCancelListener(this);
And not handling to method event onCancel(DialogInterface dialog)
public void onCancel(DialogInterface dialog) {
....
}
Because I want to make return dismiss or cancel Dialog from method of AlertDialog :
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface subDialog, int id) {
// ↓ Dismiss ExampleDialog from cancel handling.
dialog.dismiss();
}
})
.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface subDialog, int id) {
// ↓ Not return to dismiss when this button clicked.
dialog.cancel();
}
}).show();