Scenario:
public class MyApplication extends Application{...}
Three Activities, Overview, List and Map,
displaying the same data only providing different UI/UX.Data fed into Contentprovider db and frequently updated externally by a SyncAdapter,
part of another application.Read data from Contentprovider in to cursor.
Do cursor.setNotificationUri(), to make the cursor listen for db/Uri changes.Alt1. Hold the cursor in MyApplication.
Overview, List and Map then asks MyApplication for data. On a db change MyApplication holds a reference to each Activity and notifies them to ask for the data again from MyApplicati开发者_Go百科on.Alt2. Hold one cursor in each Activity.
On a db change each Activity requeries the cursor for the data again.Where should we hold the Cursor(s) ?
I'm facing a similar problem but not with DB and cursor but simple web request\response.
The problem u'll start facing soon enough is what ahppens when your activity dies in the midst of a request response.
I would suggest the following:
- make a base activity u'll inherit from that will contain a cursor for the query.
- make sure you have a method to fill the currsor with data in onResume start listening to broadcast receiver.
- when you finish a db update in the separate thread, notify all receivers.
- when you perform on create check the data in the DB to see if you missed a call from the broadcast because your activity has died.
so basically u will have a cursor in each activity, but no need to manage it by code unless you need to do something that is not standard with it. don't forget to dispose the cursor once the activity dies.
精彩评论