0

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

  //BackPress
      public void onBackPressed() {

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

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

测试对话片段

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);
    }
    }
4

1 回答 1

2

你没有show()调用DialogFragment. 在您的代码中,您只创建了一个DialogFragment.

public void onBackPressed() {

    TestDialogFragment test= new TestDialogFragment();
    test.show(getFragmentManager(), "my_dialog");

}
于 2013-07-24T02:34:34.410 回答