# Android: Asynctask causes ANR (very) intermittently

I'm doing some work on an AsyncTask (creating game objects, loading resources etc) while displaying a static splashscreen.

The problem is that every now and then, the AsyncTask doesn't run and the app becomes unresponsive.

I must point out that this is extremely difficult to reproduce. So far today, I've un-installed my app and re-launched it 73 times so far (no joke) and it's behaved perfectly. However, yesterday (again, after un-installing and installing many many times), I saw the issue about 4 times. It's very random.

What I'm asking is, if there is anything that I am doing obviously wrong here (or not doing, that I should be)?.

I am:

• Targeting Gingerbread, although so far, have only seen this issue on a tablet running Lollipop 5.0.1 (But this may simply be because this is my main test device)
• Not sure if I should be doing any 'clean up' in onDestroy?
• Am using only 1 AsyncTask - in most of the similar questions that I have read, it seems people are using more than one AsyncTask??
• Am starting it from the UI Thread

Also this problem only seems to happen when installing / running from Eclipse. (ie, hitting the run button). Once it becomes unresponsive, I can close the app and relaunch it straight from the device and it always works OK.

I have checked in onDestory and can confirm the AsyncTask's status is 'finisihed' (obviously only when the app launches OK, not when it crashes) although I'm not performing any specific clean-up work.

Any ideas?

Some Code:

In my onCreate method:

@Override
protected void onCreate(Bundle savedInstanceState) {

//Only showing code here pertienent to the question (AsyncTask Stuff)
myAsync.execute();

}

protected Void doInBackground(Void... params) {

//It appears that, when the app becomes unresponsive, this my not be reached
Log.v("tag","DoInBackground");

//Here I am just creatin game objects, loading sounds and putting some values into sharedPreferences

return null;
}

• What do you have in the 'newBundle' variable? I also load resources like this in my game and haven't noticed any problems. However, I always dispose of everything in onDestroy (bitmaps, sounds, etc.) – luben Jan 11 '15 at 18:32
• Hi @middlehut, sorry when I said I wasn't doing any cleanup, I mean with the actual AsyncTask object itself (myAsync - but I don't think there is anything I can actually do with this in that respect). I am of course, destroying all of my bitmaps etc. newBundle is basically "newBundle = new Bundle(savedInstanceState);" - this is used in the onPostExecute method, so shouldn't be related to the issue - thanks! – BungleBonce Jan 11 '15 at 18:38
• Do you have any other AsyncTasks waiting for the resource loading one? – immibis Jan 12 '15 at 4:36
• Hi @immibis no, I have only the 1 asyncTask in my app which is the one mentioned. (Loading of resources, setting values etc). I checked the status in onDestroy and it reported it was finished so that seems to be OK. Not sure what else I can check? I installed/uninstalled my app so many times today and never saw the problem. – BungleBonce Jan 12 '15 at 4:40