setOnCancelListener and setOnDismissListener is not called for AlertDialog for back button pressed or touch outside

前端 未结 5 1655
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-02 21:48

When

  • Touch outside the dialog region
  • Press on back button

I\'m expecting onDismiss (Or onCancel) will be call

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 22:45

    The problem happens when you are using DialogFragment to display Dialog

    According to http://developer.android.com/reference/android/app/DialogFragment.html, the solution is to override onCancel in DialogFragment

    Please take note from http://developer.android.com/reference/android/app/DialogFragment.html#onCreateDialog(android.os.Bundle) too

    Note: DialogFragment own the Dialog.setOnCancelListener and Dialog.setOnDismissListener callbacks. You must not set them yourself. To find out about these events, override onCancel(DialogInterface) and onDismiss(DialogInterface).

    // This is DialogFragment, not Dialog
    @Override
    public void onCancel(DialogInterface dialog) {
    }
    

提交回复
热议问题