Android:使用DialogFragment的警报对话框未显示

时间:2014-08-17 21:20:37

标签: android android-alertdialog android-dialogfragment

我正在尝试创建一个警报对话框,但它没有显示它并继续处理下一行代码。我已经彻底搜索过但无法得到任何答案。

这是我的片段非常简单

public class AlertDialogFragment extends DialogFragment {

  public static AlertDialogFragment newInstance(int title) {
    AlertDialogFragment frag = new AlertDialogFragment();
    Bundle args = new Bundle();
    args.putInt("title", title);
    frag.setArguments(args);
    return frag;
  }

  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    int title = getArguments().getInt("title");

    return new AlertDialog.Builder(getActivity())
            ./*setIcon(R.drawable.alert_dialog_dart_icon)
            .*/setTitle(title)
            .setPositiveButton("Close",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                        }
                    })
            ./*setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                        }
                    }).*/create();
  }
}

我的活动中的代码显示对话框,但它会跳过它并移动到下一行而不显示对话框弹出窗口

DialogFragment alertDialogFragment = AlertDialogFragment
                    .newInstance(R.string.alert_dialog_title);
alertDialogFragment.show(getSupportFragmentManager(), "dialog");

/**
 * Forward to next Activity
 */
Intent intent = new Intent(this, SelectResponsibilityActivity.class);
startActivity(intent);

不确定问题出在哪里。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

DialogFragment附加到其活动中。当您启动另一个活动时,您的对话框片段所附加的活动将不再可见,您的片段也是如此。

间接影响遍及this documentation

- 您可以在DialogFragment添加按钮并在onClick

中执行此操作
public void onClick (DialogInterface dialog, int whichButton){
    finish();
    startActivity(getIntent())
}

- 或者您可以使用以下代码重新加载活动,而不使用onClick,并在新活动中显示您的dialogFragment

finish();
startActivity(getIntent())

当然,在开始新活动之前,您必须设置一些标志或类似内容,并在onCreate()中检查该标志。只有设置好后,才显示对话碎片。