Android: Singleton, which is used between Activity and Service

im wondering it would be a bad idea to create a Singleton that is used between some of the actions of Android and the Android service. As far as I know, static fields, in my case Singleton, are available while the whole process is alive.

My plan is to use singleton instead of Parcelable to exchange data between my activities and the background service. So my Activity1 will add some data by calling MySingleton.getInstance (). AddData (foo); then I would send an intention to inform my service that new data has been added to singleton. Then my BackgroundService will process the intent and call MySingleton.getInstance (). GetLatestData (); then it processed the data (takes some time). The result of the service will be the next "mail" back, using singleton and starting the broadcast, which are processed by Activity1 (if they are active), and Activity1 will extract the result from singleton.

Do you guys think this is a bad idea?

EDIT: I want to implement a world of software that downloads data from a web server, analyzes it and returns a result. This way my activity will create a DownloadJob object. DownloadJob-Object will be placed in DownloadScheduler (Singleton), which queues and manages all DownloadJobs. DownloadScheduler allows you to run 5 DownloadJobs at the same time and use the queue to hold the wait. Efficient downloads will be performed using the DownloadService (IntentService), which receives information about the intent that the new DownloadJob should now be launched (downloaded) right now. The DowanlodService service will receive the next task from the DownloadSchedulers (PriorityBlockingQueue) queue and return the result by setting DownloadJob.setResult (...) and starting the broadcast intent so that the result is ready, which will be received by DownloadScheduler, which will remove the task from the queue and inform the Office that download completed etc.

So in my scenario, I would use singleton to access DownloadJobs from DownloadService instead of doing DownloadJob Parcelable and passing it with Intent. Therefore, I would avoid the problem that I have two DownloadJobs in memory (one on "Site Activity" and one on "Service site").

Any suggestions on how to solve this problem better?

Is it true that static instances like DownloadScheduler (Singleton) will be used by freed up Android systems at the bottom of the memory? So will there be a subclassification of the application and contain a link there (not a static one) to avoid this problem?

+7
source share
3 answers

If you use a singleton as a shared memory between a background service that I believe is performing operations on another thread, you may run into synchronization problems and read inconsistent data.

If the data in singleton is not synchronized, you should be careful because you rely on your β€œprotocol” to make sure that no one is reading while your background thread is writing (which may lead to errors).

On the other hand, if it is synchronized, you run the risk of encountering anr error, because the activity that reads the data may be blocked, waiting for the completion of the service to write data in singleton mode.

As the other said, you should also keep in mind that your singleton can be freed if os needs resources and that your data may not be available.

I would rather use an event bus like otto or eventbus

EDIT:

Using a singleton point as an entry point into the background mode (intention) is an approach proposed in 2010 by Vergil Dobzanski talk about creating client applications for android relaxation.

In the proposed approach, there is one singleton, which acts as a controller of current requests. Please also note that a request for service intent is already queued using os, so you can drop several intentions that will be processed sequentially by the service of intent.

Some time ago, I also tried to use this as a starting point for a library that still remains incomplete. You can find sources here.

What I certainly would not do is to store your data in singleton mode. The approach I would prefer is to store the data in some persistent storage (e.g. sql / preferences / file / content provider) and inform the client about this change through a broadcast message (or, if you use a content provider, through an observer )

Finally, to some extent, this is an approach followed by the robospice library, which looks pretty mature and provides many interesting features like caching.

+2
source

The best idea is to subclass Application and place any long live objects in it. By subclassing the application, you can correctly handle the launch and shutdown of the application, which you cannot easily handle a singleton. In addition, using Activites and Services applications, you can share access to the models of your program without resorting to additional offers. And you can avoid all the problems that Singletons face in your program.

You also do not need to resort to storing everything in the database, which requires a lot of boiler plate code in order to put a bunch of data there. It does nothing to exchange behavior between parts of your application and does nothing to facilitate communication and centralize activities. If you really need to maintain state between stops, use it, but if not, you can save a lot of work.

You can also learn something like Roboguice , which allows you to introduce shared models into your actions and services.

You may find this useful:

The design pattern principle in Android development?

+1
source

Using a single like this is not necessarily a bad idea, but you will lose it if Android decides to stop your process. You may want to save your state instead in the SQLite database or in a constant queue (look at tape for a good example).

0
source

All Articles