Loading data and updating the user interface

There are several things that push me to the process of loading data and notifying the user interface in Android. I use ( Intent) Serviceto load data that works quite well.

I am not sure how to deal with the notification of the user interface that the data has been received. After some research and discussion in Downloading data using IntentService - Lifecycle Changes and What is the best way to communicate between a service and an activity? I arrive at a point where I am not sure which method to use more.

The alternatives I'm looking for are as follows:

  • Service + LocalBroadcasts + Databases
    • An Activitywill launchService
    • Service will load data into the database
    • Servicewill send Broadcastwith a notification of 'done'
    • Activitywill get this Broadcastand retrieve data from the database usingAsyncTask
    • Activitywill retrieve data from the database with AsyncTaskeach time it is displayed to the user ( onResume, for cases where he missed the broadcast due to user transition).
  • Service + ResultReceivers + Databases
    • An Activitywill launchService
    • Service will load data into the database
    • Servicewill notify ResultReceiverwhat he did
    • Activity will retrieve data from the database using AsyncTask
    • Instead of retrieving data each time, it is ResultReceiverreused to change the life cycle, and notifications are not lost.
  • Service + ResultReceivers + Bundle
    • An Activitywill launchService
    • Service will load data (optional to the database)
    • Service ResultReceiver, , Bundle.
    • Activity Bundle ( AsyncTask).
    • , , ResultReceiver , .

1 2 / . 3, , , , Broadcast ResultReceiver ( , ).

3, , , - .

- ?


(, ) , , , , , . , , ResultReceiver Broadcast .

+4
1

- DataManager singleton, , , . ResultReceiver s, DataManager.

, ResultReceiver Broadcast , . , GC - .

0

Source: https://habr.com/ru/post/1540402/


All Articles