1

I want to show a ProgressDialog in AsyncTask. This run fantastic. But if i call mLoginPD.dissmiss() in onPostExecute() do not run.

The ProgressDialog is always on the screen.

Here is my code:

SherlockActivity mActivity;
ProgressDialog mLoginPD;

public Task_Login(String name, String pass, SherlockActivity activity) {
    this.passwort = pass;
    this.benutzername = name;
    this.mActivity = activity;
}

protected void onPreExecute() {
    super.onPreExecute();

    mLoginPD = new ProgressDialog(mActivity);
    mLoginPD.show(mActivity, "Login", "Logge Spieler ein...");
}

protected void onPostExecute(Void result) {             
    Log.e("hello", "hello");

    mLoginPD.dismiss();
    mLoginPD.cancel();
    if(mLoginPD.isShowing()) {
        mLoginPD.dismiss();
    }
}

onPostExecute() calls. I can see "hello" in LogCat.

(I have doInBackground() but i is irrelevant)

StefMa
  • 3,344
  • 4
  • 27
  • 48
  • You are calling a progress dialog from a background thread... might need to reconsider - use a message handler that is created on the main thread, and call the handler with the message to show the dialog from within the background thread, then when it ends, call the handler to tell it to dismiss the progress.. – t0mm13b Jan 20 '13 at 20:13
  • i need to use the ProgressDialog on the main thread? But it gives examples where the PD is show and dismiss on AsyncTask. But this isnt work on my code. http://stackoverflow.com/a/4538935/1231245 <- here for example. Or is it because he has an Activity and the AsnyTask is in one Java-File?! – StefMa Jan 20 '13 at 20:19
  • Some do it on AsyncTask thread, others do it on the handler by passing messages which is a cleaner way to do it IMHO... – t0mm13b Jan 20 '13 at 20:23
  • 3
    He's not calling progress dialog from a background thread, onPre* and onPost* is called on the main thread, only doInBackground is on a background thread – Johnny Sørensen Jan 20 '13 at 20:24
  • Code seems fine... Are you sure you're not adding another dialog somewhere else, or that, somehow, `onPreExecute()` gets called twice? – dmon Jan 20 '13 at 20:25
  • Why did you pass in the activity into the `new` call on the ProgressDialog? Should that not be a `Context` type? – t0mm13b Jan 20 '13 at 20:31
  • @t0mm13b Activity extends Context. – A--C Jan 20 '13 at 20:33
  • @A--C Yes... I know that... just trying to get a feeling of it, have not used SherlockActivity as that for calling a new ProgressDialog, and also without any logcat either its difficult to gauge what's going on! – t0mm13b Jan 20 '13 at 20:35
  • OP: Please post the logcat of what's happening in your code! – t0mm13b Jan 20 '13 at 20:37

3 Answers3

3

The problem is that you're creating two ProgressDialog objects.

This line:

mLoginPD = new ProgressDialog(mActivity);

creates a dialog and assigns it to mLoginPD, but does not show it.

This line:

mLoginPD.show(mActivity, "Login", "Logge Spieler ein...");

creates another dialog and shows that one. The problem is that show() is a static method that creates and shows a dialog all in one. So it's creating a second one separate from mLoginPD which is shown. mLoginPD is never shown, so calling dismiss() or cancel() doesn't do anything.

What you need to do is this:

mLoginPD = ProgressDialog.show(mActivity, "Login", "Logge Spieler ein...");

in place of both those lines. This uses show() to create and show the dialog and assign it to mLoginPD so you can dismiss it later.

Geobits
  • 22,218
  • 6
  • 59
  • 103
2

If you're overriding onPreExecute, i dont think you're supposed to call super.onPreExecute()?

0

The answer from Geobits istn running too. Always show a NullPointerException.

Here is the code to solve my problem:

mLoginPD = new ProgressDialog(mActivity);
mLoginPD.setTitle("Login");
mLoginPD.setMessage("Logge Spieler ein...");
mLoginPD.show();

than i can call mLoginDP.dismiss() or cancel() in onPostExecute()

StefMa
  • 3,344
  • 4
  • 27
  • 48