69

什么时候

  • 触摸对话区域外
  • 按下返回按钮

我期待onDismiss(Or onCancel) 将被调用。但是,它们都没有被调用。我可以知道我缺少什么吗?从AlertDialog setOnDismissListener not working,我想当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);  
4

5 回答 5

167

当您使用DialogFragment显示时会发生问题Dialog

根据http://developer.android.com/reference/android/app/DialogFragment.htmlonCancel ,解决方案是覆盖DialogFragment

请注意http://developer.android.com/reference/android/app/DialogFragment.html#onCreateDialog(android.os.Bundle )

注意:DialogFragment 拥有 Dialog.setOnCancelListener 和 Dialog.setOnDismissListener 回调。你不能自己设置它们。要了解这些事件,请覆盖 onCancel(DialogInterface) 和 onDismiss(DialogInterface)。

// This is DialogFragment, not Dialog
@Override
public void onCancel(DialogInterface dialog) {
}
于 2013-08-16T09:20:56.340 回答
11

如果您正在使用AlertDialog,请参阅

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

dialog.setCanceledOnTouchOutside(true)(感谢@LeosLiterak)

于 2015-09-15T09:43:44.020 回答
4

您还没有设置backPressed关键事件

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;
    }
});
于 2013-08-16T07:27:18.500 回答
3

您应该阅读此主题:如何通过单击对话框外部来关闭对话框?

Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true);
于 2013-08-16T08:51:04.160 回答
2

在您的 DialogFragment 中,覆盖

@Override
public void onStop() {
    super.onStop();
    if (mListener != null) {
       // do something here
    }
}
于 2015-03-19T04:02:02.573 回答