开发者

Android best practises: Loading stuff in background and storing results acceptable from multiple activities?

I am currently developing a application which is capable of locating nearby bus and train stops and display departure boards for the users.

I have a Activity, let us call it Activity1 that displays the 20 nearest stops.

When one of these are clicked, Activity2 is opened and the departure-board is loaded and shown.

In both cases, I am using AsyncTasks for data retrieval and parsing in background.

What I however would like to do is to load so开发者_Go百科me of the departure boards in the background while showing the nearest stops. These would then be available as soon as the user decides to see one.

So I would like some kind of mechanism (Thread or Service) that runs parallel with the UI Thread and loads these data. The problem is that this mechanism should be accessibly from both the activities. What is the best approach to achieve this?

I thought about creating a static class SideLoader with a Runnable object and maybe a public list of loaded departure boards that is updated contentiously when this Runnable object are executed. Is this seam like a good practice or what?


You might also look at the following video from the Google I/O conference on doing REST queries asynchronously and storing the data locally for use. Excellent description of best practices that you may learn a lot from. Talks about services, asynctask, contentproviders, and more:

http://www.youtube.com/watch?v=xHXn3Kg2IQE

And to expound on that thought - You might use a ContentProvider to access the information stored in a SQLLite database on the device. Use a service to fetch the data online and store it in the database through the contentprovider. Your activities access the contentprovider to retrieve the data.


I would look at building an Android Service to manage your background tasks. Each activity could bind to the service and communicate with it as needed.

http://developer.android.com/reference/android/app/Service.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜