Reputation: 5986
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?
MainActivity
to Service
- I assume this is a safe practice and my broadcast won't get lostSharedPreferences
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!
Upvotes: 1
Views: 169
Reputation: 8916
Most common way is sending a broadcast and also you should avoid using singletons, for more information see this link
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
Upvotes: 1