Skip to content Skip to sidebar Skip to footer

How To Reuse An Already Executing Asynctask?

Scenario : In activity1 user clicks a fileName. Activity 2 is invoked and it downloads the file showing progress. An asynctask exists ,in Activity 2,whose work is to download and

Solution 1:

Create a static object of the Asysnc task and check its status on resuming the Activity

static YourAsyncTask backgroundTask;

In onCreate()/onResume()

if(backgroundTask == null)
{
    backgroundTask = new YourAsyncTask();
    backgroundTask.execute();
}
else
{
    if(backgroundTask.getStatus() == AsyncTask.Status.PENDING){
        // AsyncTask has not started yet
    }

    if(backgroundTask.getStatus() == AsyncTask.Status.RUNNING){
        // AsyncTask is currently doing work in doInBackground()
    }

    if(backgroundTask.getStatus() == AsyncTask.Status.FINISHED){
        // AsyncTask is done and onPostExecute was called
    }
}

Hope this helps :)

Post a Comment for "How To Reuse An Already Executing Asynctask?"