简体   繁体   English

IntentService 和 Activity 生命周期

[英]IntentService and Activity Lifecycle

I followed this tutorial on how to reverse geocode in Android, but I can't understand how this IntentService will deal nicely with the Activity lifecycle.我遵循了有关如何在 Android 中反向地理编码的教程,但我无法理解此IntentService将如何很好地处理Activity生命周期。 I am following this code sample .我正在关注此代码示例

As far as I understand, the Activity will hold an instance of the AddressResultReceiver , named mResultReceiver .据我了解,该Activity将举行的一个实例AddressResultReceiver ,命名mResultReceiver When the IntentService get's back the reverse-geocoded address, it will call mReceiver.send(resultCode, bundle) from the mResultReceiver instace, which will then update the UI, inside the Activity .IntentService取回反向地理编码的地址时,它将从mResultReceiver实例调用mReceiver.send(resultCode, bundle) ,然后它将更新Activity内的 UI。

My question is how will this play along with the Activity lifecycle.我的问题是这将如何与Activity生命周期一起发挥作用。 Lets assume the Activity started the IntentService and then, by some reason, it was destroyed (like, the user rotated the screen).让我们假设Activity启动了IntentService ,然后由于某种原因,它被销毁了(例如,用户旋转了屏幕)。 Later, when the IntentService receives the address back from Google Maps, it will call the mResultReceiver it recevied from the original Activity , that is right now destroyed (or it could have been created again, but still, it will be a new instance anyway).稍后,当IntentService从谷歌地图收到地址时,它会调用它从原始Activity接收到的mResultReceiver ,即现在已销毁(或者它可以再次创建,但无论如何它仍然是一个新实例) .

Since the mResultReceiver is created when the Activity is created, and it isn't saved on any Bundle for later retrieval, when the IntentService completes and calls the original mResultReceiver , what would happen?由于mResultReceiver是在创建Activity创建的,并且没有保存在任何Bundle IntentService以后检索,当IntentService完成并调用原始mResultReceiver ,会发生什么?

  • Will the mResultReceiver call be automagically forwarded to a new instance of the Activity , like nothing happend and all work as expected? mResultReceiver调用是否会自动转发到Activity的新实例,就像什么都没发生一样,一切都按预期工作?

  • Will mResultReceiver be Garbage Collected with the destroyed Activity , and the IntentService will throw an exception when it tries to use the destroyed class? mResultReceiver会与销毁的Activity mResultReceiver被垃圾mResultReceiver ,而IntentService在尝试使用销毁的类时会抛出异常?

  • Will mResultReceiver prevent the original Activity from being garbage collected, specially because the IntentService is still holding a reference to the mResultReceiver object. mResultReceivermResultReceiver阻止原Activity被垃圾回收,特别是因为IntentService仍然持有对mResultReceiver对象的引用。 And when the IntentService calls mReceiver.send(resultCode, bundle) it will make no difference on the actual Activity on screen, or throw an exception for trying to update already destroyed UI Widgets?IntentService调用mReceiver.send(resultCode, bundle)它对屏幕上的实际Activity没有任何影响,或者抛出异常以尝试更新已销毁的 UI 小部件?

  • Is this google example flawed?这个谷歌例子有缺陷吗? What's the right way to do it then?那么正确的做法是什么?

I believe the google example is flawed.我相信谷歌的例子是有缺陷的。 The sample will result in a memory leak if activity is destroyed.如果活动被破坏,样本将导致内存泄漏。 A better approach would be using a local broadcast receiver or an Event Bus or RxJava/RxAndroid更好的方法是使用本地广播接收器或事件总线或 RxJava/RxAndroid
Issue is already reported here 问题已在此处报告

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM