DialogFragment的OnDismissListener

CaptureActivity中扫码会显示一个DialogFragment,显示扫码得到的操作选项 。DialogFragment中有个close按钮,点了隐藏,同时会调用handler.restartPreviewAndDecode(),使得可以重新扫码。现在遇到问题是显示了DialogFragment,点击手机返回按钮,一样会隐藏DialogFragment。但是不会调用handler.restartPreviewAndDecode()导致无法重复扫码。重写onBackPress或onKeyDown方法也不行,因为DialogFragment的隐藏是先于BackPress的。
解决方法:public class CaptureActivity extends AppCompatActivity implements DialogInterface.OnDismissListener
@Override
public void onDismiss(DialogInterface dialog) {
  continuePreview();
}
重写DialogFragment :
@Override
public void onDismiss(final DialogInterface dialog) {
  super.onDismiss(dialog);
  final Activity activity = getActivity();
  if (activity instanceof DialogInterface.OnDismissListener) {
  ((DialogInterface.OnDismissListener) activity).onDismiss(dialog);
  }
}
参考:
http://stackoverflow.com/questions/23786033/dialogfragment-and-ondismiss



你可能感兴趣的:(备忘笔记整理)