对于后退button的按下或触摸,不调用setOnCancelListener和setOnDismissListener

什么时候

  • 触摸对话区域外部
  • 按下后退button

我期待onDismiss (或onCancel )将被调用。 但是,他们都不叫。 我可以知道有什么我失踪? 从AlertDialog setOnDismissListener不工作 ,我认为onCancel将被称为当我按下button。 但是这对我不起作用。 我可否知道有什么我错过了?

 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 ,解决scheme是重写DialogFragmentDialogFragment

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

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

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

你还没有设置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; } }); 

如果您使用AlertDialog ,请参阅

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

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

你应该阅读这个主题: 如何通过点击对话框外的对话框来closures对话框?

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

在你的DialogFragment中,覆盖

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