Program Club

setOnCancelListener 및 setOnDismissListener는 뒤로 버튼을 누르거나 외부를 터치하는 경우 AlertDialog에 대해 호출되지 않습니다.

proclub 2020. 12. 14. 20:08
반응형

setOnCancelListener 및 setOnDismissListener는 뒤로 버튼을 누르거나 외부를 터치하는 경우 AlertDialog에 대해 호출되지 않습니다.


언제

  • 대화 영역 외부를 터치합니다.
  • 뒤로 버튼 누르기

onDismiss(Or onCancel)가 호출 될 것으로 예상 합니다. 그러나 둘 다 호출되지 않습니다. 내가 놓친 것이 있는지 알아도 될까요? 에서 에 AlertDialog setOnDismissListener가 작동하지 , 내가 생각 onCancel난 다시 버튼을 누를 때 호출됩니다. 그러나 그것은 나를 위해 작동하지 않습니다. 내가 놓친 것이 있는지 알 수 있습니까?

public class RateAppDialogFragment extends SherlockDialogFragment {
    public static RateAppDialogFragment newInstance() {
        RateAppDialogFragment rateAppDialogFragment = new RateAppDialogFragment();
        return rateAppDialogFragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.rate_app_dialog_fragment, null);

        Utils.setCustomTypeFace(view, Utils.ROBOTO_LIGHT_TYPE_FACE);

        final AlertDialog dialog = new AlertDialog.Builder(this.getSherlockActivity())            
        .setTitle("Love JStock?")
        .setView(view)
        // Add action buttons
        .setPositiveButton("Rate 5 stars \u2605", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
                Utils.showShortToast("Rate");
            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Utils.showShortToast("No");
            }
        })
        .setNeutralButton("Later", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Utils.showShortToast("Later");
            }
        })
        .create();

        dialog.setCanceledOnTouchOutside(true);

        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                Utils.showShortToast("Back button pressed?");
            }
        });

        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {

            @Override
            public void onDismiss(DialogInterface dialog) {
                // TODO Auto-generated method stub
                Utils.showShortToast("Back button pressed?");
            }
        });

        return dialog;
    }

}

    FragmentManager fm = fragmentActivity.getSupportFragmentManager();
    if (fm.findFragmentByTag(RATE_APP_DIALOG_FRAGMENT) != null) {
        return;
    }

    RateAppDialogFragment rateAppDialogFragment = RateAppDialogFragment.newInstance();
    rateAppDialogFragment.show(fm, RATE_APP_DIALOG_FRAGMENT);  

DialogFragment디스플레이에 사용할 때 문제가 발생 합니다.Dialog

에 따르면 http://developer.android.com/reference/android/app/DialogFragment.html ,이 솔루션은 무시하는 것입니다 onCancelDialogFragment

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) {
}

If you are using AlertDialog, see

builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        // dialog dismiss without button press
    }
});

and dialog.setCanceledOnTouchOutside(true) (thanks @LeosLiterak)


You have not set backPressed key event

dialog.setOnKeyListener(new Dialog.OnKeyListener() {
    @Override
    public boolean onKey(DialogInterface arg0, int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return true;
    }
});

you should read this topic: How to dismiss the dialog with click on outside of the dialog?

Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);

in your DialogFragment, override

@Override
public void onStop() {
    super.onStop();
    if (mListener != null) {
       // do something here
    }
}

참고URL : https://stackoverflow.com/questions/18267916/setoncancellistener-and-setondismisslistener-is-not-called-for-alertdialog-for-b

반응형