So, I've been debugging an issue in my game, where if I start then stop it repeatedly, I have noticed that sometimes an Asynctask doesn't seem to get started, even though I appear to do the right thing to get it started. I've removed a lot of unnecessary code to show what happens, but basically I'm seeing that I'm about to start an Asynctask, but the task never gets started.
public void onStart()
{
super.onStart();
Log.d(TAG,"About to execute");
Play_level.execute();
}
AsyncTask<Void,Integer,Integer> Play_level=new AsyncTask<Void, Integer, Integer>(){
protected void onProgressUpdate(Integer ...time) {
//Stuff goes in here.
}
@Override
protected Integer doInBackground(Void... params) {
Log.d(TAG,"In Task");
}
};
And the logcat shows:
03-10 19:28:16.983: D/Level(1535): About to execute
03-10 19:28:17.503: D/Level(1535): onStop()
03-10 19:28:17.993: D/Level(1535): onPause()
03-10 19:28:19.174: D/AJEG(1535): Starting Tongue
03-10 19:28:19.313: D/Level(1535): ImageList Previously Loaded
03-10 19:28:19.313: D/Level(1535): About to execute
03-10 19:28:19.853: D/Level(1535): onStop()
03-10 19:28:20.283: D/Level(1535): onPause()
03-10 19:28:21.464: D/AJEG(1535): Starting Tongue
03-10 19:28:21.604: D/Level(1535): ImageList Previously Loaded
03-10 19:28:21.604: D/Level(1535): About to execute
03-10 19:28:22.134: D/Level(1535): onStop()
03-10 19:28:22.404: D/Level(1535): onPause()
03-10 19:28:23.504: D/AJEG(1535): Starting Tongue
03-10 19:28:23.644: D/Level(1535): ImageList Previously Loaded
03-10 19:28:23.644: D/Level(1535): About to execute
03-10 19:28:24.184: D/Level(1535): onStop()
Furthermore, no amount of re-entering the task seems to be able to re-start the code, as is shown by the last bit of text.
Just to give a bit more context, Starting Tongue is played in the onStart() of the parent activity (AJEG), Level is the name of the activity I'm starting. The ImageList command can be safely ignored, but I've included it for completeness. Part of the doInBackground includes the text "Starting Level _", where _ is the level that is about to be started. onStop() and onPause() are in the method to show when onStop() and onPause() occur in the Level task.
So, the solution to the problem is as stated in this blog article:
AsyncTask uses a static internal work queue with a hard-coded limit of 10 elements
So, essentially I had an AsyncTask that never finished, to the amount of 10, by re-starting my thread so much. To get around this, I did the following:
doInBackground.The result is something like this:
private LevelPlay Play_level;
public void onStop()
{
super.onStop();
Play_level.cancel(true);
}
public void onStart()
{
super.onStart();
Play_level=new LevelPlay();
Play_level.execute();
}
class LevelPlay extends AsyncTask<Void, Integer, Integer>
{
protected void onProgressUpdate(Integer ...time) {
}
@Override
protected Integer doInBackground(Void... params) {
Log.d(TAG,"In Task");
Boolean keepRunning=true;
while(keepRunning && !isCancelled ())
{
//DoStuffHere
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With