GetView DialogFragment returns null on on theDismiss

I have a user dialog derived from DialogFragment.

When the user clicks OK, I need to save the information that is on the screen.

So, I turned off my PositiveButton calls, and I implemented the onDismiss method to save the data.

In the onDismiss method, I need to get the data from the editView, which is in the dialog box. I am using getView (). FindViewByID to get editView, but the GetView () method 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 that I can save a view inflated in OnCreateDialog as an attribute, but it seems wrong to me.

How can I get a view from the screen in onDismiss?

Ps: the place where I work does not allow me to send my code, so I took the code from Google and I changed it as close as possible to my code.

+4
source share
2 answers

Old but gold. This allows you to have more control over the 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 that I can save a view inflated in OnCreateDialog as an attribute, but it seems wrong to me.

Yes, this one. This looks right, especially when implementing things like seekBar and when using libraries like ButterKnife.

+2
source

The way I executed my login DialogFragment was using the callback method for the parent fragment action, such as:

 builder.setPositiveButton("Login", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { EditText username = (EditText) getDialog().findViewById(R.id.username); EditText password = (EditText) getDialog().findViewById(R.id.password); un = username.getText().toString(); pw = password.getText().toString(); if (un.equals("") || pw.equals("")) { Toast.makeText(getActivity(), "Username or Password field was empty", Toast.LENGTH_SHORT).show(); //Don't login & do something (I made a recursive callback to the fragment that created the dialog) } else if (!un.equals("username") || !pw.equals("password")) { Toast.makeText(getActivity(), "Username or Password was incorrect", Toast.LENGTH_SHORT).show(); //Don't login & do something (I made a recursive callback to the fragment that created the dialog) } else if (un.equals("username") && pw.equals("password")) { Toast.makeText(getActivity(), "You have logged in successfully", Toast.LENGTH_SHORT).show(); mLogin.Login(); } 

Then, the Login() callback method will create the next fragment that is needed.

The snippet that will create your login dialog will have the following code in its callback method:

 LoginDialog login = new LoginDialog(); login.show(getFragmentManager(), "LOGIN"); 
+3
source

Source: https://habr.com/ru/post/1482258/


All Articles