I have a custom dialog derived from DialogFragment.
When the user click in the OK button I need to save the information that is on the screen.
So I made my PositiveButton calls dismiss and I implemented the method onDismiss to save the data.
In the onDismiss method I need do get the data from the editView that is on the Dialog. I'm using getView().findViewByID to get the editView, but the method GetView() returns null.
Here is my code:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().dismiss();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
@override
public void onDismiss(){
EditView view = (EditView)getView().findViewByID(R.id.edit);
}
I know I can save the view inflated in the OnCreateDialog as a attribute, but that doesn't seems right to me.
How is the right way to get the view from the screen in the onDismiss?
Ps: the place where I work don't allow me to post my code, so I took a code from google and I changed it to be as close as possible of my code.
Show activity on this post. tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog.
Showing the DialogFragment It is not necessary to manually create a FragmentTransaction to display your DialogFragment . Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
you can set your args. class IntervModifFragment : DialogFragment(), ModContract. View { companion object { fun newInstance( plom:String,type:String,position: Int):IntervModifFragment { val fragment =IntervModifFragment() val args = Bundle() args. putString( "1",plom) args.
Old but gold. This one allows to have bigger control over whole fragment (for example when implementing seekBar or using ButterKnife). Enough said:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_signin, null);
// do your stuff with views here
builder.setView(view)
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().dismiss();
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
I know I can save the view inflated in the OnCreateDialog as a attribute, but that doesn't seems right to me.
Yeah, that's the one. It looks right though, especially when implementing things like seekBar and while using libraries like ButterKnife.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With