Skip to content Skip to sidebar Skip to footer

Disabling Re-execution Of Async Task On Finishing Of Foreground Activity

Case: I am using Async Task for showing some list. When I click on any item of listview another activity comes foreground of last activity. This foreground activity contains a fini

Solution 1:

then start your asynctask in oncreate method of first activity when second activity start the first activity will become foreground and when visible second activity finjsh then first activity will not be recreate it will only resume the pause activity so as u called asynctask in oncreate it will not start thread again.

Solution 2:

Well this is something you should achieve using the singleton class

  1. Create a class say DataManager

    publicclassDataManager {
    publicstaticDataManager dManager;
    
    boolean isDataRecieved;
    
    publicstaticDataManagergetInstance()
    {
        if(dManager!=null)
            return dManager;
    
        else
            dManager=newDataManager();
    
        return dManager;
    }
    
    }
    

here the getInstance method ensures there is only one object of this class and the boolean isDataRecieved should be set to true as soon as the data is parsed and stored.

like on postExecute of your ASyncTask add these lines

Datamanager d=Datamanager.getInstance();
d.isDataRecieved=true;

now on onCreate add these lines

Datamanager d=Datamanager.getInstance();
if(!d.isDataRecieved)
{
//This means data is not recieved 

execute your asynctask elsenot 
}

Post a Comment for "Disabling Re-execution Of Async Task On Finishing Of Foreground Activity"