Sticky broadcasts rebroadcast

1.7k views Asked by At

I want to change the extras of a sticky broadcast, does another call to sendStickyBroadcast with the same intent creates a new sticky broadcast or replaces the previous one?

Also, I use sticky broadcast to maintain a state of some property and share the state to whoever wants it (services and activities), Is there a better approach to share a state of some property between all my android classes?

1

There are 1 answers

2
Tal Kanel On BEST ANSWER

does another call to sendStickyBroadcast with the same intent creates a new sticky broadcast or replaces the previous one?

it replaces the current one in case that the Intent is filter equal to the previous one that been already broadcast

I would not use Sticky broadcast because it been deprecated from security reasons from android 5.

also, from my experience - using sticky broadcast for the reason you describes can cause very easy to tons of bugs and unexpected behaviors if you are not considering every case the onReceive() can be called...

Is there a better approach to share a state of some property between all my android classes?

yes. there are much better ways to save state across different application components, depends on the specific use case:

  • save the state as a field (or object) within a singleton class. implementing class as a singleton provide global access to it from any other application component. the singleton state will save as long as your process is alive..

  • in case that you need to save the state and restore it even after the application process stops, you should save the state in one of the persistent ways to do so: SharedPreference is a good choice for saving persistently primitive types like strings, integers..

  • storing the state with Sqlite database whould be the choice for more complex objects and lists..