Niju Mt
Niju Mt

Reputation: 159

multiple asyntask

This is somewhat design specific question but I am looking for right advice.

In my app I have 6 pages A,B,C... each of which will download data from server and display it. In this case among below mentioned approaches which one will be good.

1.For each A,B,C write inner asynctask class 2.write separate asyntasks like asyntaskA,asyntskB etc for each of these activities 3. write a single asynctask and route request of each activity through a requestcontroller class which creates instance of asyctask by passing context,url parameters

If the second approach is taken, is it possible to run one Activity and another Activity in onpause state while its asynctask still running?

Upvotes: 0

Views: 139

Answers (2)

gnuanu
gnuanu

Reputation: 2260

In each page, if you are doing the same kind of operation (As in this case, downloading data and displaying it), you need not write separate AsyncTask classes. Just create as much instances as you need and call the execute() method. AsyncTask class will handle this in thread safe way.

Second part of your question is bit unclear, but if you meant switching between activities, it is possible with multiple instances of a single AsyncTask. You'll need to save and restore the activity states.

If the operation you performing is time consuming, say more than few seconds, it's not safe using an AsyncTask. The better option will be a Service.

Upvotes: 1

Ridcully
Ridcully

Reputation: 23655

There's no problem with multiple AsyncTasks. In one of my apps I have a grid-view with images and there's an AsyncTask for every image loading and generating a thumbnail image. They all run in parallel quite nicely.

Upvotes: 0

Related Questions