title: Long running AsyncTasks for Fragments in a ViewPager
body: <p>I have an activity powered by a <code>FragmentPagerAdapter</code> which adds fragments as the user swipes left and right.</p>
  
  <p>This child fragment that is added is currently static and contains a <code>ListView</code> of items. I intend to use and <code>AysncTask</code> to get items for the <code>ListView</code> but this process can take as much as 10-15 seconds. </p>
  
  <p>I'm still lost with a few things:</p>
  
  <p>From which method should my <code>AsyncTask</code> be invoked? Would it be the <code>onCreate</code> method of the <code>Fragment</code>? I'd only like the <code>AsyncTask</code> to be invoked when the Fragment is in focus.</p>
  
  <p>The biggest question however is, what happens to my <code>AysncTask</code> when the user swipes away from the screen while the <code>AsyncTask</code> is loading. Is the Activity destroyed? Is there a way I can prevent the Activity from being destroyed? If I don't do this, all the progress made by the <code>AsyncTask</code> is lost and I have to reinvoke the <code>AsyncTask</code> when that <code>Fragment</code> comes into focus.</p>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>Reason for slowness:</p>
  
  <p>I'm scraping a page with about a 150 records and jSoup is really, really slow. The problem with not using a service is that the data has a high probability of changing and I don't want to "pre-scrape" the data. </p>
  
  <p>@Fildor suggested using a <code>Loader</code> to do the heavy lifting. A quick look at the <code>Loader</code> implementation showed that allows you persist data across short durations, like orientation change. I'm still wondering whether the <code>ViewPager</code>'s swing across <code>Fragments</code> would allow the data to persist in the <code>Loader</code> momentarily. </p>