rbcc rbcc - 4 months ago 13
Android Question

AsyncTask never finishes when other tasks are running

In my app, I show a menu screen immediately to the user and then use an

AsyncTask
to load some data from disk. Loading takes about 3 seconds if that's all the app had to do. When the user hits a menu option and the data hasn't loaded yet, a "Please wait, loading" screen appears.

I have two problems:


  • I recently added ads to my app and have found this is making my app hang sometimes because the
    AsyncTask
    never finishes loading. What seems to be happening is that the ad banner (I'm using AdMob and this happens when AdSense ads are shown which AdMob do occasionally) is using resources by playing a simple animation and the
    AsyncTask
    never gets a chance to finish.

  • I want to play a simple looping animation (e.g. making a view rotate) on my menu screen and this makes the
    AsyncTask
    take about 5 times longer to finish which renders the background loading pointless.



How can I avoid situations where my background data never finishes loading or has such a low priority it takes a long time?

I can't seem to find a way to increase the priority of an
AsyncTask
.

Answer

In AsyncTask's onPostExecute(List<type> result), check if the result is not equal to null (meaning that the dialog was dismissed) and then populate your result in the main class.

Edit

Actually you have to send the result from doInBackground() to your onPostExecute(). Then only it will callonPostExecute()`.

Comments