0

I have:
One bindable service setup to hand out android.os.Messenger's.
Two Fragment inside one activity bind to the service and receive their Messengers.
Both Fragments are active at the same time as they are part of a ViewPager.

When dispatching a message to both Fragments at the same time, The app crashes because of ANR

code that sends the message: (from this example)

public void sendMessageToUI(Message msg) {
        for (int i = mClients.size() - 1; i >= 0; i--) {
            try {
                msg.replyTo = mMessenger;
                Messenger client = mClients.get(i);
                client.send(msg);
            } catch (RemoteException e) {
                mClients.remove(i);
            }
        }
    }

More info
After extensive testing I've concluded that the messenging system works when targeting just the first, or second fragment, but crashes when targeting both.

Community
  • 1
  • 1
Tom Bevelander
  • 1,686
  • 1
  • 22
  • 31

1 Answers1

0

Somewhere in the guts of the messenger, messages are apparently unique to the messenger they are being sent to. If you look at the example on the android docs here: (RemoteMessengerServiceSample) you'll see that a new message must be obtained PER messenger

mValue = msg.arg1;
for (int i=mClients.size()-1; i>=0; i--) {
  try {
        mClients.get(i).send(Message.obtain(null,
        MSG_SET_VALUE, mValue, 0));
  } catch (RemoteException e) {
        // The client is dead.  Remove it from the list;
         // we are going through the list from back to front
        // so this is safe to do inside the loop.
        mClients.remove(i);
  }
}