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
)Service
s 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
Activity
would start theService
- The
Service
would download the data into a database - The
Service
would send out theBroadcast
with a 'done' notification - The
Activity
would pick up thisBroadcast
and retrieve the data from the database using anAsyncTask
- The
Activity
would retrieve the data from the database using anAsyncTask
every 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
Activity
would start theService
- The
Service
would download the data into a database - The
Service
would notify theResultReceiver
that it's done - The
Activity
would retrieve the data from the database using anAsyncTask
- Instead of retrieving the data every time, the
ResultReceiver
is reused across lifecycle changes, and notifications are not lost.
- An
- Service + ResultReceivers + Bundle
- An
Activity
would start theService
- The
Service
would download the data (optionally into a database) - The
Service
would notify theResultReceiver
that it's done, and supplies the data in aBundle
. - The
Activity
would retrieve the data from theBundle
(NoAsyncTask
needed). - Instead of retrieving the data every time, the
ResultReceiver
is 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 Broadcast
s or ResultReceiver
s 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 ResultReceiver
s or Broadcast
s for sending result data is recommended against.