title: Android asynctask show progress dialog until function has finished
body: <p>I know this answer has already been answered multiple times but I cannot get it to work properly. It's starts with activity A where a user needs to login via the function authenticate. When the user hits the button login I want the progressdialog to appear until the function authenticate has got a response. While success you go to another activity where you can navigate through the app.. Anyway sometimes when the internet is slow or if you're on mobile internet I still see activity A for a few seconds doing nothing while the auth function is going on. When it's done the system hops to activity B.</p>
  
  <p>I tried using sleep thread thingy but that's not the point.. I want the progressdialog to appear when the user hits login and dissapear when the auth function has been finished.</p>
  
  <p>With the help of AsyncTask I sometimes see the dialog for one flash of a second but not how it should be.. I also want to use asynctask for loading my listviews but the dialog isn't doing what it should.</p>
  
  <p>Here my AsyncTask code</p>
  
  <pre><code>public class HeavyWorker extends AsyncTask&lt;String, Context, Void&gt; {
  
  private ProgressDialog dialog;
  
  public HeavyWorker() {
  }
  
  @Override
  protected void onPreExecute() {
      super.onPreExecute();
      dialog = new ProgressDialog(AuthenticationActivity.this);
      dialog.setMessage("Gettting data");
      dialog.setIndeterminate(true);
      dialog.setCancelable(false);
      dialog.show();
  }
  
  @Override
  protected Void doInBackground(String... params) {
          authenticate(username, password, autoLogin);
  
      return null;
  }
  
  @Override
  protected void onPostExecute(Void result) {
      if (dialog != null &amp;&amp; dialog.isShowing()) {
          dialog.dismiss();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And where I call the asynctask</p>
  
  <pre><code>builder.setTitle(R.string.dialog_authenticate)
                  .setIcon(R.drawable.ic_launcher)
                  .setView(viewInflater)
                  .setPositiveButton(R.string.dialog_button_login,
                          new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog,
                                      int whichButton) {
                                  EditText ETusername = (EditText) viewInflater.findViewById(R.id.username);
                                  EditText ETpassword = (EditText) viewInflater.findViewById(R.id.password);
  
                                  CheckBox optionAutoLogin = (CheckBox) viewInflater
                                          .findViewById(R.id.autoLogin);
  
                                  checkAutoLogin = 0;
                                  if (optionAutoLogin.isChecked()) checkAutoLogin = 1;
                                  username = ETusername.getText().toString();
                                  password = ETpassword.getText().toString();
  
                                  new HeavyWorker().execute();
                                  //authenticate(ETusername.getText().toString(), ETpassword.getText().toString(), checkAutoLogin);
                              }
                          })
  </code></pre>
  
  <p>Any help or advice is appreciated.</p>