2

I have an AsyncTask in my program that seems to stop after onPreExecute(), after the app is run several times. What I mean is, I can use the app and everything works about 10-20 runs, but then it halts. Here is what's going on:

private class MyTask extends AsyncTask<String, Void, Data> {

    protected void onPreExecute() {
        // show loading dialog
        Log.d(TAG, "end onPreExecute");
    }

    protected Data doInBackground(String... params) {
        Log.d(TAG, "start doInBackground");
        // do stuff
    }

    protected void onPostExecute(Data myData) {
        // do stuff
    }

}

When it stops working, what happens is that the loading dialog just keeps loading forever, and "end onPreExecute" prints but "start doInBackground" does not.

Why might that be?

Kalina
  • 5,504
  • 16
  • 64
  • 101
  • Do you dismiss your dialog in "onPostExecute"? – Chris Oct 11 '12 at 22:32
  • There is nothing on LogCat that you can attach to your question? Maybe it could be a problem of OutOfMemory, because you are running a lot of times... Without more information it's impossible to help you. []s Neto – Neto Marin Oct 11 '12 at 22:57
  • Is there something else that you are executing in PreExecute that can stall? Connecting to a service, or loading something? – Aleksandar Stojadinovic Oct 11 '12 at 23:15
  • @NetoMarin no, logcat does not display anything other than what I wrote in Log.d() – Kalina Oct 12 '12 at 13:52
  • @Alexander.S no, the loading dialog is the only thing I do in onPreExecute – Kalina Oct 12 '12 at 13:54
  • @ChrisConway sort of... I'm using just a ProgressBar (just the circle itself, not in an actual dialog). In onPreExecute I set its visibility to VISIBLE, and in onPostExecute I set the visibility to GONE. – Kalina Oct 12 '12 at 13:59

1 Answers1

1

It's hard to tell where the problem is. As your Async Task seems to work the first couple of times, it could be that this API-feature is just too "unstable" for your intention...

A solution for your issue could simply be, to replace the AsyncTask by a different Thread solution:

Hope that helps!

Community
  • 1
  • 1
Chris
  • 4,403
  • 4
  • 42
  • 54
  • That is helpful, thanks! I didn't realise AsyncTask isn't right for all cases. I'm trying to go with the traditional thread alternative; but do you know how I can still do "preExecute" and "postExecute" stuff with that? – Kalina Oct 15 '12 at 14:48
  • Try using a callback. Could get called at the beginning, in the middle and at the end of your thread in the method which runs on the UI-Thread. Therefore your callback object could implement an interface with methods like "preExecute", "progressUpdate" and "postExecute" where you pass some parameters and define what you want to do on the UIThread... http://stackoverflow.com/questions/3398363/how-to-define-callbacks-in-android http://saigeethamn.blogspot.com/2010/04/threads-and-handlers-android-developer.html – Chris Oct 17 '12 at 20:57