2

In an application I have an Activity, a Service (doing some stuff regardless of whether or not the activity is visible), and a Model class (in MVC sense), it is a singleton. The activity and the service play the role of Controllers, the View is defined in the xml layout (nothing custom, the existing classes are re-used). Both Controllers use the same Model. The service is not remote, that is, lives in the same process.

There are a number of techniques (e.g. here, here, here) recommended for communication between an Activity and a Service, common data structures are not among them (Google says it is possible, but that's in a section that does not even mention broadcasts, messengers, binding and other service-specific stuff). Well, I understand issues with multiple threads, but if I care about the access from multiple threads myself, why on Earth do I need other methods?

(UPD: I have checked, in fact, the service and the activity even use the same thread: "main", id=1.)

Question: Is the use of common data structures for communication between activities and services considered a bad style? If yes, why?

So far I am inclined to think that this method is a valid one.

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • if both Activity and Service are in the same process use bound local service pattern, read more here http://developer.android.com/guide/components/bound-services.html then ^F local – pskink Nov 27 '14 at 13:33

1 Answers1

0

Singleton class does the job for me. I'm using a SingletonActivity in my projects and never experienced any locking or other thread-specific problems.

The simplicity and transparency of such an approach is way more important than being "bad styled" or soever, and it doesn't introduce any IPC overhead, as the "standard ways" do. Your code remains as clean as YOU want it, and not as Google does >)

injecteer
  • 20,038
  • 4
  • 45
  • 89