Android - DialogFragment没有显示

时间:2013-07-24 02:30:01

标签: java android android-dialogfragment

我想在按下按钮时显示一个对话框,但下面的代码没有执行并显示FragmentDialog。提前谢谢。

  //BackPress
      public void onBackPressed() {

          TestDialogFragment test= new TestDialogFragment();
          test.newInstace();
            // Showing Alert Message

          Log.d("Video Backpressed", "sdfdf");
      }

TestDialogFragment

public static class TestDialogFragment extends DialogFragment {

    public static DialogFragment newInstace() {
        DialogFragment dialogFragment = new TestDialogFragment();
        return dialogFragment;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("TestDialogFragment");
        builder.setView(getContentView());
        Dialog dialog = builder.create();
        return dialog;
    }

    private View getContentView() {
        LayoutInflater inflater = getActivity().getLayoutInflater();
        return inflater.inflate(R.layout.dialog_fragment, null);
    }
    }

1 个答案:

答案 0 :(得分:2)

您没有调用show()的{​​{1}}方法。在您的代码中,您只创建了DialogFragment的实例。

DialogFragment