I'm pretty desperate. That's the situation: I have an activity and a service. Communication from service to activity relies on a pretty simple ResultReceiver.
public class ServiceReceiver extends ResultReceiver {
public ServiceReceiver(android.os.Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
//do stuff, update UI using runOnUIThread()
}
}
Everything works fine: activity receives asynchronous messages and updates the UI successfully. But if I rotate the screen, I lose the connection: I know that the "old" ResultReceiver is lost and I'm fine with it, the thing is that I create and send a new one to the service. I do this onCreate:
Intent sIntent = new Intent(this, ConnectionService.class);
serviceReceiver = new ServiceReceiver(new Handler());
sIntent.putExtra("receiver", serviceReceiver);
bindService(sIntent, mConnection, Context.BIND_AUTO_CREATE);
and the service:
public void onRebind(Intent intent) {
serviceReceiver = intent.getParcelableExtra("receiver");
Log.e("SERVICE", "REBIND");
}
successfully prints "REBIND" every time the screen is rotated. serviceReceiver is where I send stuff: if I set it to null onRebind (but not onStartCommand or onBind, so I'm sure this happens after a configchange) , I obtain a nullpointerexception when I try to send things, so I guess it actually gets updated (ie gets the "new" ResultReceiver from the new activity).
I don't want to "save" the old ResultReceiver and "rewire" it when the activity is recreated; I'm ok with creating a new ResultReceiver and pass it to the service onRebind(), which will update its. But it's not working.
After screen rotation and rebind, the onReceiveResult() of the "new" ResultReceiver can't access UI - the stuff it changes don't show up. I don't know where to look for an error: everything works until the screen is rotated, so it must be related to the service holding reference to something old, I guess.