Share info between two processes - what's the safest way?

173 views Asked by At

I have an app with two processes - the MainActivity and a background Service. I need the same information in both, and this info comes from my server (a JSON response). I was loading this JSON from both processes, but as this JSON may get bigger it's pretty inefficient to load twice.

What is the safest or best practice to share this info?

  • Broadcast and copy the JSON from MainActivity to Service - I assume this is a safe practice and my broadcast won't get lost
  • Use SharedPreferences to store this locally and both consume from this "local" JSON until the app is closed. According to this question SharedPreferences works fine between processes.

Any other suggestion/recommendation is very appreciated.

Thanks in advance!

1

There are 1 answers

1
Saeed Masoumi On BEST ANSWER

Most common way is sending a broadcast and also you should avoid using singletons, for more information see this link

Android and RESTful services

It's better to take a look at Event Bus

As the doc says :

simplifies the communication between components

  • decouples event senders and receivers
  • performs well with Activities, Fragments, and background threads
  • avoids complex and error-prone dependencies and life cycle issues