jonathanrz
jonathanrz

Reputation: 4296

getView's DialogFragment returning null on onDismiss

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.

Upvotes: 5

Views: 6036

Answers (2)

Darek Deoniziak
Darek Deoniziak

Reputation: 773

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.

Upvotes: 3

TronicZomB
TronicZomB

Reputation: 8747

The way that I did my login DialogFragment was by using a callback method to the fragment's parent activity like such:

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 callback method Login() will create the next fragment that is needed.

The fragment that will create your login dialog will have the following code within its callback method:

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

Upvotes: 3

Related Questions