App expect Alertdialog response to password

Asked

Viewed 515 times

1

The function below returns a password typed in AlertDialog and is in a class responsible for creating any dialog box in my app, but as a whole AlertDialog the app does not "wait" the return of the function to continue the execution. My question is: how to make the app wait for the function to return to continue executing the rest of the code?

    public String pass() {
    final String[] result = new String[1];

    final EditText txtUrl = new EditText(activity);

    // Set the default text to a link of the Queen
    txtUrl.setHint("digite a senha");

    new AlertDialog.Builder(activity)
            .setTitle("Senha")
            .setMessage("Digite a senha para liberar o acesso")
            .setView(txtUrl)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    result[0] = txtUrl.getText().toString();

                }
            })
            .setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            })
            .show();
    return result[0];
}
  • There is a this class, which does exactly that, but however not a good practice, do not recommend to use. Java is different for example from C# and other languages, you can pass a listener for its function.

  • 1

    Look at this reply. If you don’t want to use a Dialogfragment (I recommend you use it), see Fragment as your class and method onAttach() as your builder.

  • @ramaral poe an example of Dialogfragment as a response so I can positive it

  • Did you understand what is in the other answer? If yes and that is what you were looking for I vote for this question as a duplicate of the other. Remember that you can always vote on any answer/question if you think it was helpful.

  • Yes understood and also Fis some queries from the information passed there. You can mark as repeated. Thank you.

1 answer

0

And if you call the next function only when the user gives the OK?

Example:

.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    result[0] = txtUrl.getText().toString();
                    activity.someFunction();

                }
            })
  • Unfortunately I can not do so, the password request will occur only when the discount exceeds a limit and the code after it should always be executed the password is only to allow this discount or return it to the default value

Browser other questions tagged

You are not signed in. Login or sign up in order to post.