There are couple of things that are bugging me in the process of downloading data and notifying the UI in Android. I'm using (Intent)Services to download the data, which works good enough.
I'm not sure how to deal with notifying the UI that data has been retrieved though. After some research and discussion in Downloading data using IntentService - Lifecycle Changes, and Which is the best way to communicate between service and activity?, I'm arriving at a point where I'm not sure which method to use anymore.
The alternatives I'm looking at are:
- Service + LocalBroadcasts + Databases
- An
Activitywould start theService - The
Servicewould download the data into a database - The
Servicewould send out theBroadcastwith a 'done' notification - The
Activitywould pick up thisBroadcastand retrieve the data from the database using anAsyncTask - The
Activitywould retrieve the data from the database using anAsyncTaskevery time it is shown to the user (onResume, for cases it has missed the broadcast due to the user navigating away).
- An
- Service + ResultReceivers + Databases
- An
Activitywould start theService - The
Servicewould download the data into a database - The
Servicewould notify theResultReceiverthat it's done - The
Activitywould retrieve the data from the database using anAsyncTask - Instead of retrieving the data every time, the
ResultReceiveris reused across lifecycle changes, and notifications are not lost.
- An
- Service + ResultReceivers + Bundle
- An
Activitywould start theService - The
Servicewould download the data (optionally into a database) - The
Servicewould notify theResultReceiverthat it's done, and supplies the data in aBundle. - The
Activitywould retrieve the data from theBundle(NoAsyncTaskneeded). - Instead of retrieving the data every time, the
ResultReceiveris reused across lifecycle changes, and notifications are not lost.
- An
Options 1 and 2 require the user waiting for the database read/write operations. Option 3 is therefore quicker, but I've seen many sources recommend not using Broadcasts or ResultReceivers to transfer data (I'm not sure exactly why though).
For now, I am sticking with option 3, but I'm not sure if this is actually a proper approach, and if I'm missing something.
Can someone shed some light on this?
While some people (possibly righteously) vote this question to be opinion based, I am looking for pitfalls I am possibly overlooking. Furthermore, I'm looking for the answer as to why using ResultReceivers or Broadcasts for sending result data is recommended against.