Sharon Haim Pour
Sharon Haim Pour

Reputation: 6723

Alert dialog closes without pressing the button

I'm building an android app with a signup activity. When the signup is successful, I want to show an alertbox with a success message and go back to the parent activity. The problem is that the alertbox is shown only for a brief time and then I go back immediately to the parent activity, without pressing any button in the alertbox. my code is:

case RESPONSE_USER_SIGNUP_SUCCESS:              
            showAlertBoxSignupSuccess();
            Intent returnIntent = new Intent();
            returnIntent.putExtra("email", email);
            setResult(RESULT_OK, returnIntent);        
            this.finish();
            break;

private void showAlertBoxSignupSuccess() 
{
    AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
    alertbox.setTitle("The account was successfuly created");
    alertbox.setNegativeButton("OK", new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) 
        {
            arg0.dismiss();
        }
    });
    alertbox.show();


}

My question is, how can I make it wait until the the user clicks the button in the alertbox and then go to the parent activity?

Thank you!!

Upvotes: 1

Views: 1579

Answers (1)

McStretch
McStretch

Reputation: 20675

If you want to wait until the user presses the OK button to leave the current activity, you should move the code beneath showAlertBoxSignupSuccess(); into your button's onClick listener:

    public void onClick(DialogInterface arg0, int arg1) 
    {
        arg0.dismiss();
        Intent returnIntent = new Intent();
        returnIntent.putExtra("email", email);
        setResult(RESULT_OK, returnIntent);        
        this.finish();
    }

Upvotes: 5

Related Questions