What is the best way to make an activity (if there is any) react to a broadcast that was send over a LocalBroadcastManager
while the activity was paused or stopped. I couldn't find much details if the LocalBroadcastManager
differs from a normal broadcast, but it seems that is still gets called event if the activity is in background. But that leads to some problems since some UI modifications are not possible as long as the activity is not shown.
That's why I would prefer to handle all broadcasts when the activity gets back to top. But how can I do this. I was thinking of a queue that will take a list of runnables that gets executed when the activity comes back to running state. But I think this is some sort of overkill since android surly does have a mechanism for that.
So whats the proper way to do this?
What I've done in the past is have my activities register a receiver onResume and unregister onPause.
When you've finished creating your certificates save to local storage whatever information you need to update your view and send out the broadcast.
If the activity is running it will receive the broadcast and update itself. If it it is paused, you should read the local storage onResume and update accordingly.
After all I found a solution that fits my needs. I create a BroadcastReceiver that is used as a subclass of my activity. Whenever the activity enters a paused state, it will call .pause() on my BroadcastReceiver. From that point all incoming Intents are put into a queue and will be flushed on a call to .unpause() from the activity.
Why don't you simply unregister the receiver onDestroy? So while activity is paused, you can still execute code in the receiver?
来源:https://stackoverflow.com/questions/15014217/how-to-make-activity-react-to-local-broadcasts-while-paused