服务回调引用旧的“幽灵”我的活动实例
我遇到了一个奇怪的问题,困扰了我几天。首先是一些背景信息:
我有一个绑定到第三方服务的活动(作为图书馆提供给我)。每当客户端请求执行操作时,该服务都会向客户端(我的活动)发送异步回调。服务永远不会通过调用 startService() 和 stopService() 来启动;当我需要服务时我执行绑定,当我完成时取消绑定。我在任何时候都不会有超过一项活动与该服务绑定。
我的问题:
用户启动我的活动(我们称之为实例 A)。我在 onCreate() 中绑定到服务并发出请求。我收到来自服务的回调并相应地设置一些局部变量。一旦我完成了我需要做的事情,我就调用 finish()。 onDestroy() 被调用,然后我执行取消绑定服务。我此时的假设是,服务实例和我的活动都不应该再被引用。
现在,A 完成后,用户再次启动我的活动(我们将此实例称为 B)。我再次绑定 onCreate 并发出请求。然而,当我收到回调时,我注意到该服务已向我的旧“幽灵”实例 A 发出回调。我现在使用实例 A 的所有局部变量,而不是使用这个新实例 B。我什至打印出“这个“在绑定之前和收到回调后再次进行引用,并且能够看到引用从实例 B 更改为实例 A。我的活动中的执行继续进行,但由于我的局部变量现在在 A 执行结束时被错误地设置为其旧状态,因此 B 的执行失败。
所以有几个问题:
- A 之后怎么可能仍然被引用 onDestroy() 和 unbind 被调用?
- 此问题是否可能与第三方有关 服务而不是我的实施? (再说一次,我没有 该服务的源代码,我只是在使用它)
感谢你们提供的任何见解。
-杰森
I've encountered a weird issue that has gotten me stumped for a few days now. First some background info:
I have an activity that binds to a third party service (given to me as a library). This service sends asynchronous callbacks to the client (my activity) whenever the client requests an action be performed. The service is never started with startService() and stopService() calls; I perform bind when i need the servcie and unbind when I am done. I never have more than one activity bound to the service at any one time.
My issue:
The user starts my activity (lets call it instance A). I bind to the service in onCreate() and issue a request. I receive a callback from the service and set some local variables accordingly. Once i go through what i need to do, i call finish(). onDestroy() gets called and i then perform un bind service. My assumption at this point is that both the Service instance and my activity should not be able to be referenced any more.
Now, after A has finished, the user starts my activty again (lets call this instance B). I again bind in onCreate and issue a request. However when i receive a callback, i notice that the service has issued a callback to my old "ghost" instance A. I am now using all of instance A's local variables instead of using This new instance B. I even printed out the "this" reference before binding and again after receiving the callback and was able to see that the reference changes from instance B's to instance A's. Execution in my activity continues to proceed, but since my local variables are now set incorrectly to their old state at the end of A's execution, B's execution fails.
So a few questions:
- How is it possible that A is still being referenced after
onDestroy() and unbind is called? - Is it likely that this issue could be related to the third party
service and not my implementation? (again, I don't have the
source code to this service, i'm just using it)
Thanks for any insights you all can offer.
-Jason
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原来这是第三方服务。
Turns out it was the the third party service.