How to use a DialogFragment to return either true or false?

Viewed 271

I have a password dialog class that ask password when ever they want to delete something or add. What i want is that can reuse this dialog again and again.
Heres my password dialog class looks like

public static class myPasswordDialog extends DialogFragment {

        @NonNull
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            LayoutInflater inflater = getActivity().getLayoutInflater();
            builder.setView(inflater.inflate(R.layout.password_dialog, null))
                    .setPositiveButton(R.string.delete, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            EditText passField = (EditText) ((AlertDialog) dialog).findViewById(R.id.password);
                            String password = passField.getText().toString();
                            if (password.matches(getString(R.string.pass_key))) {

                                //it should do something when it is correct
                            } else {
                                Toast.makeText(getActivity(), "Wrong Password", Toast.LENGTH_SHORT).show();
                            }
                        }
                    })
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                        }
                    });
            return builder.create();
        }
    }

and this is how I call my password class

deletebtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myPasswordDialog mPasswordDialog = new myPasswordDialog();
                        mPasswordDialog.show(getFragmentManager(), "delete");
                    }
                });

What I have in mind is that the fragment will return a boolean if they inputted the right or wrong password. But in what way can I do it?

1 Answers
Related