我想在按下返回按钮时显示一个对话框,但是下面的代码无法执行并显示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);
}
}
最佳答案
您没有调用show()
的DialogFragment
方法。在您的代码上,您仅创建了DialogFragment
的实例。
public void onBackPressed() {
TestDialogFragment test= new TestDialogFragment();
test.show(getFragmentManager(), "my_dialog");
}