Android 在片段中显示DialogFragment
我一直试图在片段中显示DialogFragment,但没有成功。这是我的代码:Android 在片段中显示DialogFragment,android,fragment,Android,Fragment,我一直试图在片段中显示DialogFragment,但没有成功。这是我的代码: @Override public void onResume() { super.onResume(); Log.d(TAG, "onResume"); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user != null && !user.isEmailVerified())
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume");
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null && !user.isEmailVerified()) {
InfoDialogFragment dialog = new InfoDialogFragment();
dialog.show(getActivity().getSupportFragmentManager(), "InfoDialogFragment");
}
}
每当这个片段出现在屏幕上时,屏幕就会变暗,当我触摸屏幕时,它会恢复正常的对比度,但我看不到对话框片段。
我错过了什么
以下是DialogFragment的代码:
public class InfoDialogFragment extends DialogFragment {
private InfoDialogListener listener;
@Override
public void onAttach(Context activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the InfoDialogListener so we can send events to the host
listener = (InfoDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement DeleteListListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
View layout = inflater.inflate(R.layout.fragment_info, null);
TextView message = (TextView) layout.findViewById(R.id.message);
message.setText(getActivity().getString(R.string.user_not_verified));
//
Button ok = (Button) layout.findViewById(R.id.ok_button);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.onDialogOKClick(InfoDialogFragment.this);
}
});
return builder.create();
}
public interface InfoDialogListener {
void onDialogOKClick(DialogFragment dialog);
}
}
感谢使用
getChildFragmentManager
而不是getActivity().getSupportFragmentManager()
。这些是不同的实例。事实上,我在InfoDialogFragment类中缺少一行代码
builder.setView(layout);
我花了两天时间才弄明白。
谢谢您的回答。您可以在InfoDialogFragment类中创建一个构造函数,并可以如下所示:
DialogFragment newFragment = new SelectDateFragment(NextDate);
newFragment.show(getFragmentManager(), "DatePicker");
您应该有一个函数,在片段的调用活动中显示一个对话框片段——使用FragmentInteractionListener模式,google将其放入样板片段/活动代码中。听起来像是试图显示对话框,但将InfoDialogFragment代码放在此处我们可以看到InfoDialogFragment代码吗?您是否尝试使用getChildFragmentManager()这很关键。非常感谢。从片段中,我们应该使用childFragmentManager