Question

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:

  1. Service + LocalBroadcasts + Databases
    • An Activity would start the Service
    • The Service would download the data into a database
    • The Service would send out the Broadcast with a 'done' notification
    • The Activity would pick up this Broadcast and retrieve the data from the database using an AsyncTask
    • The Activity would retrieve the data from the database using an AsyncTask every time it is shown to the user (onResume, for cases it has missed the broadcast due to the user navigating away).
  2. Service + ResultReceivers + Databases
    • An Activity would start the Service
    • The Service would download the data into a database
    • The Service would notify the ResultReceiver that it's done
    • The Activity would retrieve the data from the database using an AsyncTask
    • Instead of retrieving the data every time, the ResultReceiver is reused across lifecycle changes, and notifications are not lost.
  3. Service + ResultReceivers + Bundle
    • An Activity would start the Service
    • The Service would download the data (optionally into a database)
    • The Service would notify the ResultReceiver that it's done, and supplies the data in a Bundle.
    • The Activity would retrieve the data from the Bundle (No AsyncTask needed).
    • Instead of retrieving the data every time, the ResultReceiver is reused across lifecycle changes, and notifications are not lost.

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.

Was it helpful?

Solution

Carefully using some sort of DataManager singleton instance, backed up by a database seems to be a robust solution. Using ResultReceivers, the UI gets notified and pulls the data from the DataManager.

I have also found out that using ResultReceivers and Broadcasts to send data has a negative impact on performance, because the objects need to be serialized. This is a costly operation, and GC might kick in because of it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top