How to make activity react to local Broadcasts while paused?

不羁岁月 提交于 2019-12-07 18:10:42

问题


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?


回答1:


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.




回答2:


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.




回答3:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!