1

There is basically a database handler and the UI and the foreground service need to communicate constantly using the Messenger to update the database and synchronize themselves. It'd be really convenient if I could share this particular object with the foreground service and the UI. How do I do that(if there is a way)? In particular, I'd like to access it's variables, methods etc. It is going to be something that is to be used across different activities, processes and services but for instance I'm just taking it to work with a foreground service.

Also, if they are not shared,i.e an individual copy of them exists ,if I close the database from either the foreground service or the UI, will the database be closed wholly or do I need to close it twice?

juztcode
  • 1,196
  • 2
  • 21
  • 46
  • `message.obj`, is it what you want? – sajjad Aug 04 '20 at 08:58
  • @Saj , message.obj , please could you mention the class or reference to the docs? do you know the answer to the second part? – juztcode Aug 04 '20 at 09:10
  • add more details to your question! – kelvin Aug 06 '20 at 08:52
  • @kelvin, I added more description. Please tell me what details more should I add. – juztcode Aug 06 '20 at 09:22
  • There has to be a good way to do every thing ! what exactly you are trying to achieve? cant you create a helper class that deals with the method calls and all . – kelvin Aug 06 '20 at 09:32
  • I mean to share an object, you need to have a sharable space. Else I'll have to use messenger to communicate back and forth so many times. It's good but shared memory would be better – juztcode Aug 06 '20 at 11:18
  • What about static variable ? – kelvin Aug 06 '20 at 11:41
  • statics aren't shared between processes, and I think services and Activities don't share a common address space, even though they virtually do, there's no way for us but to use IPC mechanisms – juztcode Aug 06 '20 at 11:46
  • What about bounded service that has getter method for `Messenger`? – Thracian Aug 11 '20 at 05:25
  • 1
    The most basic way to do this is via an object in the Application class: https://stackoverflow.com/a/1945297/4672107. You have access to context in a service the same as you do in an activity. However more commonly in large applications you would use a separate singleton (sometimes provided by the DI library like dagger) – Carson Holzheimer Aug 12 '20 at 04:41

0 Answers0