I am getting DeadObjectException in my code. Below, I describe my setup, and what I have tried to fix the issue.
A Service S is started from Application A.
Activity B (from another application) communicates with that service using Messenger and IBinder interfaces. This activity is also started from the same service. On a button press inside this activity, I am supposed to send a message back to the service. I frequently get DeadObjectException on button press.
I understand that the most likely cause for this is - system killed and restarted the service, and the activity has old reference to it. Beyond this, I haven't found much information online.
This is what I have tried:
startForeground() in service, and I could see that the service was running (notification in notification area) continuously. I still got the exception.BroadcastReceiver, and calls finish() in onReceive()). The problem is, if the activity pauses, Intents are not delivered to it. Also, I see onDestroy() being hit in a nutshell, because Android manages activity life cycle, intents are not guaranteed to be delivered to it. I also tried extending a BaseActivity as mentioned in this stackoverflow question.
FLAG_ACTIVITY_CLEAR_TASK (in addition to usual FLAG_ACTIVITY_NEW_TASK), with no better results.Now, I feel as if I am out of options. Has anyone faced similar problems? Is there something I can try? Is it possible to catch the exception and then rebind the service?
In Activity B, implement an IBinder.DeathRecipient interface (possibly wrapped in a custom class) and register it with the IBinder from your service via IBinder.linkToDeath(). Your callback will be hit when/if the Service dies for some reason and Activity B can gracefully cleanup any internal book keeping it has as well as re-bind to the service.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With