活动/片段暂停时如何处理处理程序消息

我的其他帖子略有变化


基本上我有一个消息Handler在我的Fragment接收一串可导致对话消息被解雇或示出。


当应用程序进入后台时,我得到一个提示,onPause但仍然如我所愿地通过我的消息。但是,因为我使用的是片段,所以我不能只关闭并显示对话框,因为这将导致IllegalStateException。


我不能只是解雇或取消允许状态丢失。


鉴于我有一个问题,Handler我想知道是否有关于在暂停状态下如何处理消息的推荐方法。


我正在考虑的一种可能的解决方案是记录暂停时通过的消息并在上播放它们onResume。这有点不能令人满意,我想框架中必须有一些东西可以更优雅地处理它。


慕运维8079593
浏览 500回答 3
3回答

蛊毒传说

quickdraw出色的PauseHandler的一个稍微简单的版本是/**&nbsp;* Message Handler class that supports buffering up of messages when the activity is paused i.e. in the background.&nbsp;*/public abstract class PauseHandler extends Handler {&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Message Queue Buffer&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private final List<Message> messageQueueBuffer = Collections.synchronizedList(new ArrayList<Message>());&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Flag indicating the pause state&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; private Activity activity;&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Resume the handler.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public final synchronized void resume(Activity activity) {&nbsp; &nbsp; &nbsp; &nbsp; this.activity = activity;&nbsp; &nbsp; &nbsp; &nbsp; while (messageQueueBuffer.size() > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Message msg = messageQueueBuffer.get(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messageQueueBuffer.remove(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendMessage(msg);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Pause the handler.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; public final synchronized void pause() {&nbsp; &nbsp; &nbsp; &nbsp; activity = null;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Store the message if we have been paused, otherwise handle it now.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param msg&nbsp; &nbsp;Message to handle.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; @Override&nbsp; &nbsp; public final synchronized void handleMessage(Message msg) {&nbsp; &nbsp; &nbsp; &nbsp; if (activity == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final Message msgCopy = new Message();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; msgCopy.copyFrom(msg);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; messageQueueBuffer.add(msgCopy);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; processMessage(activity, msg);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Notification message to be processed. This will either be directly from&nbsp; &nbsp; &nbsp;* handleMessage or played back from a saved message when the activity was&nbsp; &nbsp; &nbsp;* paused.&nbsp; &nbsp; &nbsp;*&nbsp; &nbsp; &nbsp;* @param activity&nbsp; Activity owning this Handler that isn't currently paused.&nbsp; &nbsp; &nbsp;* @param message&nbsp; &nbsp;Message to be handled&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected abstract void processMessage(Activity activity, Message message);}它确实假定您始终要存储脱机消息以进行重播。并提供“活动”作为输入,#processMessages因此您无需在子类中对其进行管理。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android