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


98

我的其他帖子略有变化

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

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

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

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

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


1
您可以在片段的onPause()方法中删除处理程序中的所有消息,但是存在恢复消息的问题,我认为这是不可能的。
Yashwanth Kumar,

Answers:


167

尽管Android操作系统似乎没有一种机制可以充分解决您的问题,但我相信此模式确实提供了一种相对简单的解决方法。

下列类是一个包装器,android.os.Handler它在活动暂停时缓冲消息并在恢复时回放消息。

确保您拥有的任何异步更改片段状态的代码(例如,提交,关闭)仅从处理程序中的消息中调用。

PauseHandler课程中派生您的处理程序。

每当您的活动接到onPause()电话PauseHandler.pause()和for onResume()PauseHandler.resume()

将您的Handler实现替换handleMessage()processMessage()

提供一个storeMessage()总是返回的简单实现true

/**
 * Message Handler class that supports buffering up of messages when the
 * activity is paused i.e. in the background.
 */
public abstract class PauseHandler extends Handler {

    /**
     * Message Queue Buffer
     */
    final Vector<Message> messageQueueBuffer = new Vector<Message>();

    /**
     * Flag indicating the pause state
     */
    private boolean paused;

    /**
     * Resume the handler
     */
    final public void resume() {
        paused = false;

        while (messageQueueBuffer.size() > 0) {
            final Message msg = messageQueueBuffer.elementAt(0);
            messageQueueBuffer.removeElementAt(0);
            sendMessage(msg);
        }
    }

    /**
     * Pause the handler
     */
    final public void pause() {
        paused = true;
    }

    /**
     * Notification that the message is about to be stored as the activity is
     * paused. If not handled the message will be saved and replayed when the
     * activity resumes.
     * 
     * @param message
     *            the message which optional can be handled
     * @return true if the message is to be stored
     */
    protected abstract boolean storeMessage(Message message);

    /**
     * Notification message to be processed. This will either be directly from
     * handleMessage or played back from a saved message when the activity was
     * paused.
     * 
     * @param message
     *            the message to be handled
     */
    protected abstract void processMessage(Message message);

    /** {@inheritDoc} */
    @Override
    final public void handleMessage(Message msg) {
        if (paused) {
            if (storeMessage(msg)) {
                Message msgCopy = new Message();
                msgCopy.copyFrom(msg);
                messageQueueBuffer.add(msgCopy);
            }
        } else {
            processMessage(msg);
        }
    }
}

以下是有关如何使用PausedHandler该类的简单示例。

单击按钮后,将延迟的消息发送到处理程序。

处理程序在UI线程上收到消息时,将显示DialogFragment

如果PausedHandler未使用该类,则在按下测试按钮以启动对话框之后,如果按下主屏幕按钮,则会显示IllegalStateException。

public class FragmentTestActivity extends Activity {

    /**
     * Used for "what" parameter to handler messages
     */
    final static int MSG_WHAT = ('F' << 16) + ('T' << 8) + 'A';
    final static int MSG_SHOW_DIALOG = 1;

    int value = 1;

    final static class State extends Fragment {

        static final String TAG = "State";
        /**
         * Handler for this activity
         */
        public ConcreteTestHandler handler = new ConcreteTestHandler();

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRetainInstance(true);            
        }

        @Override
        public void onResume() {
            super.onResume();

            handler.setActivity(getActivity());
            handler.resume();
        }

        @Override
        public void onPause() {
            super.onPause();

            handler.pause();
        }

        public void onDestroy() {
            super.onDestroy();
            handler.setActivity(null);
        }
    }

    /**
     * 2 second delay
     */
    final static int DELAY = 2000;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (savedInstanceState == null) {
            final Fragment state = new State();
            final FragmentManager fm = getFragmentManager();
            final FragmentTransaction ft = fm.beginTransaction();
            ft.add(state, State.TAG);
            ft.commit();
        }

        final Button button = (Button) findViewById(R.id.popup);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final FragmentManager fm = getFragmentManager();
                State fragment = (State) fm.findFragmentByTag(State.TAG);
                if (fragment != null) {
                    // Send a message with a delay onto the message looper
                    fragment.handler.sendMessageDelayed(
                            fragment.handler.obtainMessage(MSG_WHAT, MSG_SHOW_DIALOG, value++),
                            DELAY);
                }
            }
        });
    }

    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
    }

    /**
     * Simple test dialog fragment
     */
    public static class TestDialog extends DialogFragment {

        int value;

        /**
         * Fragment Tag
         */
        final static String TAG = "TestDialog";

        public TestDialog() {
        }

        public TestDialog(int value) {
            this.value = value;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            final View inflatedView = inflater.inflate(R.layout.dialog, container, false);
            TextView text = (TextView) inflatedView.findViewById(R.id.count);
            text.setText(getString(R.string.count, value));
            return inflatedView;
        }
    }

    /**
     * Message Handler class that supports buffering up of messages when the
     * activity is paused i.e. in the background.
     */
    static class ConcreteTestHandler extends PauseHandler {

        /**
         * Activity instance
         */
        protected Activity activity;

        /**
         * Set the activity associated with the handler
         * 
         * @param activity
         *            the activity to set
         */
        final void setActivity(Activity activity) {
            this.activity = activity;
        }

        @Override
        final protected boolean storeMessage(Message message) {
            // All messages are stored by default
            return true;
        };

        @Override
        final protected void processMessage(Message msg) {

            final Activity activity = this.activity;
            if (activity != null) {
                switch (msg.what) {

                case MSG_WHAT:
                    switch (msg.arg1) {
                    case MSG_SHOW_DIALOG:
                        final FragmentManager fm = activity.getFragmentManager();
                        final TestDialog dialog = new TestDialog(msg.arg2);

                        // We are on the UI thread so display the dialog
                        // fragment
                        dialog.show(fm, TestDialog.TAG);
                        break;
                    }
                    break;
                }
            }
        }
    }
}

storeMessage()PausedHandler类添加了一个方法,以防止即使暂停活动也应立即处理任何消息。如果处理了一条消息,则应返回false,并且该消息将被丢弃。


26
好的解决方案,请客。尽管不禁想到框架应该处理这个问题。
PJL

1
如何将回调传递给DialogFragment?
Malachiasz

我不确定我是否理解Malachiasz问题,请您详细说明。
quickdraw mcgraw 2014年

这是一个非常优雅的解决方案!除非我做错了,否则由于在技术上resume使用该方法,sendMessage(msg)因此可能在消息循环之前(或循环的迭代之间)有其他线程将消息排队,这意味着可以将存储的消息与到达的新消息交错。不知道这有什么大不了的。也许使用sendMessageAtFrontOfQueue(当然,向后迭代)可以解决此问题?
yan 2014年

4
我认为这种方法可能并不总是有效-如果操作系统破坏了活动,则恢复后待处理的消息列表将为空。
加拉佩塔2015年

10

quickdraw出色的PauseHandler的一个稍微简单的版本是

/**
 * Message Handler class that supports buffering up of messages when the activity is paused i.e. in the background.
 */
public abstract class PauseHandler extends Handler {

    /**
     * Message Queue Buffer
     */
    private final List<Message> messageQueueBuffer = Collections.synchronizedList(new ArrayList<Message>());

    /**
     * Flag indicating the pause state
     */
    private Activity activity;

    /**
     * Resume the handler.
     */
    public final synchronized void resume(Activity activity) {
        this.activity = activity;

        while (messageQueueBuffer.size() > 0) {
            final Message msg = messageQueueBuffer.get(0);
            messageQueueBuffer.remove(0);
            sendMessage(msg);
        }
    }

    /**
     * Pause the handler.
     */
    public final synchronized void pause() {
        activity = null;
    }

    /**
     * Store the message if we have been paused, otherwise handle it now.
     *
     * @param msg   Message to handle.
     */
    @Override
    public final synchronized void handleMessage(Message msg) {
        if (activity == null) {
            final Message msgCopy = new Message();
            msgCopy.copyFrom(msg);
            messageQueueBuffer.add(msgCopy);
        } else {
            processMessage(activity, msg);
        }
    }

    /**
     * Notification message to be processed. This will either be directly from
     * handleMessage or played back from a saved message when the activity was
     * paused.
     *
     * @param activity  Activity owning this Handler that isn't currently paused.
     * @param message   Message to be handled
     */
    protected abstract void processMessage(Activity activity, Message message);

}

它确实假定您始终要存储脱机消息以进行重播。并提供“活动”作为输入,#processMessages因此您无需在子类中对其进行管理。


为什么是resume()and pause()handleMessage synchronized
Maksim Dmitriev 2014年

5
因为您不希望在#handleMessage中调用#pause并突然发现在#handleMessage中使用该活动时该活动为空。这是跨共享状态的同步。
威廉

@William请您详细解释一下为什么您需要在PauseHandler类中进行同步吗?此类似乎只能在一个线程(UI线程)中工作。我猜想在#handleMessage期间无法调用#pause,因为它们都在UI线程中工作。
Samik

@威廉你确定吗?HandlerThread handlerThread = new HandlerThread(“ mHandlerNonMainThread”); handlerThread.start(); Looper looperNonMainThread = handlerThread.getLooper(); 处理程序handlerNonMainThread =新的Handler(looperNonMainThread,新的Callback(){public boolean handleMessage(Message msg){return false;}});
swooby

抱歉,@ swooby,我不明白。我确定吗?您发布的代码段的目的是什么?
威廉

2

这是解决在回调函数中执行Fragment提交并避免IllegalStateException问题的方法的稍微不同的方法。

首先创建一个自定义的可运行接口。

public interface MyRunnable {
    void run(AppCompatActivity context);
}

接下来,创建一个片段来处理MyRunnable对象。如果MyRunnable对象是在Activity暂停后创建的,例如旋转屏幕或用户按下home按钮,则将其放入队列中,以便以后使用新上下文进行处理。由于setRetain实例设置为true,因此该队列可以保留所有配置更改。方法runProtected在UI线程上运行,以避免带有isPaused标志的竞争状态。

public class PauseHandlerFragment extends Fragment {

    private AppCompatActivity context;
    private boolean isPaused = true;
    private Vector<MyRunnable> buffer = new Vector<>();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = (AppCompatActivity)context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }

    @Override
    public void onPause() {
        isPaused = true;
        super.onPause();
    }

    @Override
    public void onResume() {
        isPaused = false;
        playback();
        super.onResume();
    }

    private void playback() {
        while (buffer.size() > 0) {
            final MyRunnable runnable = buffer.elementAt(0);
            buffer.removeElementAt(0);
            new Handler(Looper.getMainLooper()).post(new Runnable() {
                @Override
                public void run() {
                    //execute run block, providing new context, incase 
                    //Android re-creates the parent activity
                    runnable.run(context);
                }
            });
        }
    }
    public final void runProtected(final MyRunnable runnable) {
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(isPaused) {
                    buffer.add(runnable);
                } else {
                    runnable.run(context);
                }
            }
        });
    }
}

最后,该片段可以在主应用程序中使用,如下所示:

public class SomeActivity extends AppCompatActivity implements SomeListener {
    PauseHandlerFragment mPauseHandlerFragment;

    static class Storyboard {
        public static String PAUSE_HANDLER_FRAGMENT_TAG = "phft";
    }

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        ...

        //register pause handler 
        FragmentManager fm = getSupportFragmentManager();
        mPauseHandlerFragment = (PauseHandlerFragment) fm.
            findFragmentByTag(Storyboard.PAUSE_HANDLER_FRAGMENT_TAG);
        if(mPauseHandlerFragment == null) {
            mPauseHandlerFragment = new PauseHandlerFragment();
            fm.beginTransaction()
                .add(mPauseHandlerFragment, Storyboard.PAUSE_HANDLER_FRAGMENT_TAG)
                .commit();
        }

    }

    // part of SomeListener interface
    public void OnCallback(final String data) {
        mPauseHandlerFragment.runProtected(new MyRunnable() {
            @Override
            public void run(AppCompatActivity context) {
                //this block of code should be protected from IllegalStateException
                FragmentManager fm = context.getSupportFragmentManager();
                ...
            }
         });
    }
}

0

在我的项目中,我使用观察者设计模式来解决此问题。在Android中,广播接收者和意图是这种模式的实现。

我要做的是创建一个BroadcastReceiver,我在片段/活动的onResume中注册,然后在片段/活动的onPause中取消注册。。在BroadcastReceiveronReceive方法中,我将需要运行的所有代码放入-BroadcastReceiver-接收通常发送到您的应用程序的Intent(消息)。要提高片段可以接收的意图类型的选择性,可以使用意图过滤器以下示例中。

这种方法的优点是意图可以从应用程序中的任何位置(在片段顶部打开的对话框,异步任务,另一个片段等)中的任何地方发送(消息)。参数甚至可以作为意向附加信息传递。

另一个优点是该方法与任何Android API版本兼容,因为在API级别1上引入了BroadcastReceivers和Intent。

除非您打算使用sendStickyBroadcast(需要在其中添加BROADCAST_STICKY的地方),否则不需要对应用的清单文件设置任何特殊权限。

public class MyFragment extends Fragment { 

    public static final String INTENT_FILTER = "gr.tasos.myfragment.refresh";

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        // this always runs in UI Thread 
        @Override
        public void onReceive(Context context, Intent intent) {
            // your UI related code here

            // you can receiver data login with the intent as below
            boolean parameter = intent.getExtras().getBoolean("parameter");
        }
    };

    public void onResume() {
        super.onResume();
        getActivity().registerReceiver(mReceiver, new IntentFilter(INTENT_FILTER));

    };

    @Override
    public void onPause() {
        getActivity().unregisterReceiver(mReceiver);
        super.onPause();
    }

    // send a broadcast that will be "caught" once the receiver is up
    protected void notifyFragment() {
        Intent intent = new Intent(SelectCategoryFragment.INTENT_FILTER);
        // you can send data to receiver as intent extras
        intent.putExtra("parameter", true);
        getActivity().sendBroadcast(intent);
    }

}

3
如果在Pause状态期间在notifyFragment()中的sendBroadcast()被调用,则unregisterReceiver()将已经被调用,因此没有接收者会抓住这个意图。如果没有代码可以立即处理,那么Android系统会否放弃此意图?
史蒂夫·B

我认为绿色机器人Eventbus粘性帖子很酷。
j2emanue
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.