Dagger2范围和活动生命周期 [英] Dagger2 scopes and activity lifecycle

查看:96
本文介绍了Dagger2范围和活动生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android活动,正在使用Dagger2将Presenter注入其中.我希望Presenter能够保持状态,即使配置发生更改.

例如,我将使用演示者启动网络通话,如果用户在进行网络通话时旋转设备,我希望能够在设备完成后收到响应它的旋转,而不必重新启动呼叫.

我被绊倒了,因为如果我将Presenter的实例范围限定在Activity的生命范围内,那么当Activity在配置更改期间通过onDestroy()进行操作时,Presenter不会被垃圾回收吗?我的另一个想法是使用在应用程序生命周期内有效的范围.但是,如果这样做,我如何确保一旦Activity被永久销毁(不是由于配置更改,而是按下返回按钮之类的东西),就可以对Presenter进行垃圾收集?

是否有一种方法可以确保我的Presenter可以在Activity的配置更改中保留下来,并且在应用程序的生命周期内也不会泄漏?

解决方案

我强烈建议您不要尝试实现此方法.

您正在有效地尝试使用DI框架以支持特定于Activity的生命周期流程,尽管DI框架并非旨在像这样使用.

我最近回答了另一个类似的问题,其中OP试图在不同的Activities之间共享视图模型中的状态.尽管用例不完全相同,但一般模式是相同的-尝试将流控制职责委托给DI框架,这不是一个好主意.

在您的情况下(IMHO)最好的方法是在旋转之前存储当前状态,在旋转时重新初始化演示者,然后恢复其状态.

在轮换期间如何存储状态取决于您要保留的确切内容:

  • 如果您需要保留与UI相关的状态(选择,文本,元素的位置等),则可以使用常见的onSaveInstanceState()onRestoreInstanceState()回调
  • 如果您需要保留一些与业务相关的状态(正在进行的网络请求,数据,数据修改等),则将此逻辑封装在业务类(例如SomeBusinessUseCaseManager)中,并从Application宽组件中注入该类,并使用一个范围.

您可以在此处中找到有关Dagger范围的详细评论. /p>

有关Android中DI的更多信息,请参见此处.

. >

I have an Android Activity that I'm using Dagger2 to inject a Presenter into. I'd like my Presenter to be capable of holding state even if a configuration change occurs.

For instance, I'm going to use the Presenter to kick off a network call and if the user rotates the device while the network call is in-flight I'd like to be able to receive the response after the device finishes its rotation and not have to restart the call.

I'm getting tripped up because if I scope the instance of Presenter to the Activity's life, then isn't there a chance that the Presenter would be garbage collected when the Activity goes through onDestroy() during a configuration change? My other thought was to use a scope that is valid during the life of the application. However, if I do that how do I ensure that my Presenter can be garbage collected once the Activity has been destroyed for good (not due to a config. change, but something like the back button being pressed)?

Is there a way to ensure that my Presenter will survive an Activity's configuration change and also not be leaked for the life of the Application?

解决方案

I would strongly advice against trying to implement this approach.

You're effectively trying to use DI framework in order to support Activity specific life-cycle flow, although DI frameworks are not intended to be used like this.

I recently answered another similar question in which OP tried to share state in View-Model between different Activities. Although use cases are not identical, the general pattern is the same - attempt to delegate flow control responsibilities to DI framework, which is not a good idea.

The best approach in your case (IMHO) would be to store the current state before rotation, re-instantiate the presenter upon rotation, and then restore its state.

How you store the state during rotation depends on what exactly you're trying to preserve:

  • If you need to preserve UI related state (selections, texts, position of elements, etc.) then you can use the usual onSaveInstanceState() and onRestoreInstanceState() callbacks
  • If you need to preserve some business related state (ongoing network requests, data, data modifications, etc.) then encapsulate this logic in a business class (e.g. SomeBusinessUseCaseManager) and inject this class from Application wide component with a scope.

You can find a detailed review of Dagger's scopes here.

More information about DI in Android can be found here.

这篇关于Dagger2范围和活动生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆