Reputation: 44978
I have an activity powered by a FragmentPagerAdapter
which adds fragments as the user swipes left and right.
This child fragment that is added is currently static and contains a ListView
of items. I intend to use and AysncTask
to get items for the ListView
but this process can take as much as 10-15 seconds.
I'm still lost with a few things:
From which method should my AsyncTask
be invoked? Would it be the onCreate
method of the Fragment
? I'd only like the AsyncTask
to be invoked when the Fragment is in focus.
The biggest question however is, what happens to my AysncTask
when the user swipes away from the screen while the AsyncTask
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 AsyncTask
is lost and I have to reinvoke the AsyncTask
when that Fragment
comes into focus.
Thanks.
Reason for slowness:
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.
@Fildor suggested using a Loader
to do the heavy lifting. A quick look at the Loader
implementation showed that allows you persist data across short durations, like orientation change. I'm still wondering whether the ViewPager
's swing across Fragments
would allow the data to persist in the Loader
momentarily.
Upvotes: 1
Views: 1562
Reputation: 1662
Use FragmentStatePagerAdapter you can simply start asyncTask from onCreateView of fragment
Upvotes: 0