I’d like to maintain a hash table in a broadcast receiver. If I understand BroadcastReceiver’s life cycle currently it could get killed wiping out my member variables. What would be the ideal strategy for retrieving a hash table from a previous run of onReceive in the BroadcastReceiver?
Share
There are two ways to use a
BroadcastReceiver, and you did not indicate which you are using.One for a receiver registered by some other component — like an
Activity— viaregisterReceiver(). This receiver will live for as long as it is registered, and so its data may last for more than oneonReceive()call. The component that registered the receiver would be responsible for persisting the data.The other is to register your receiver in the manifest. Those, per the quoted passage in cdonner’s answer, will go away after a single
onReceive()call. Your receiver will need to persist its data itself, to a database, flat file, or whatever.