-1

I'm trying to present the user with a login dialog (custom dialog with custom view), and when then click "Login", i'm trying to show a ProgressDialog, then close it once the login has completed.

Problem is i can't get the ProgressDialog to show up.

Here's my code:

    private final MyActivity activity;

    public MyPresenter(MyActivity activity) {
        this.activity = activity;
    }

    public Dialog getLoginSignupDialog() {
        final Dialog dialog = new Dialog(activity);
        dialog.setContentView(R.layout.dialog_signup);
        dialog.setTitle("Login");
        final TextView email = (TextView)dialog.findViewById(R.id.textViewEmail);
        final TextView password = (TextView)dialog.findViewById(R.id.textViewPassword);
        Button signupButton = (Button)dialog.findViewById(R.id.buttonSignup);           
        signupButton.setOnClickListener(new OnClickListener() {                 
            public void onClick(View v) {   
                ProgressDialog progressDialog = null;

                try {
                    progressDialog = ProgressDialog.show(activity, "", "Signing up...",false);
                    activity.signup(email.getText().toString(), password.getText().toString());
                    progressDialog.dismiss();
                    dialog.dismiss();
                } catch (ParseException e) {
                    if (progressDialog != null) progressDialog.dismiss();
                    Toast.makeText(activity, "Sorry, an error occured.", Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });     
        return dialog;
    }

This code is in a Presenter, which get's passed the main activity instance. Just to keep the UI logic seperate.

In the main activity (onCreate), i do this:

showDialog(DIALOG_SIGNUP);

Then i override onCreateDialog method:

@Override
protected Dialog onCreateDialog(int id) {   
    Dialog dialog = null;   
    switch (id) {
        case DIALOG_SIGNUP: {
            dialog = presenter.getLoginSignupDialog();                              
            break;
        }
    }   
    return dialog;

}

The other dialog (custom login one) gets shown fine, everything works, closes, etc. But i can't get the ProgressDialog to show.

What am i doing wrong?

RPM1984
  • 72,246
  • 58
  • 225
  • 350

2 Answers2

0

I think it would help to put your progress dialog in your function activity.signup() and also dismiss it there. This should create the dialog in the main ui thread.

Perhaps you should use an AsyncTask for your login process.

JackTools.Net
  • 734
  • 6
  • 13
0

You should do the sign-up activity as an AsyncTask as follows:

    new AsyncTask<String, Void, Void>() {


        ProgressDialog progressDialog=null;


        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(activity, "", "Signing up...",false);
        }

        @Override
        protected Void doInBackground(String... params) {
            try {
                activity.signup(params[0], params[1]);

            } catch (ParseException e) {
                Toast.makeText(activity, "Sorry, an error occured.", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (progressDialog != null) progressDialog.dismiss();
            dialog.dismiss();
        }
    }.execute(email.getText().toString(), password.getText().toString());
Sameer
  • 4,379
  • 1
  • 23
  • 23