作者:北若久 | 来源:互联网 | 2023-05-18 06:22
Assumingwhathavebeensaidhere,itsthedevelopersresponsibilitytokeepthecomponentinstanc
Assuming what have been said here, it's the developer's responsibility to keep the component instance in order to implement their own scope logic (since a scoped method will return the same instance for a given component).
假设这里已经说过,开发人员有责任保留组件实例以实现自己的范围逻辑(因为范围方法将为给定组件返回相同的实例)。
What's the clean way to keep this component reference through the activity lifecycle ?
通过活动生命周期保持此组件引用的干净方法是什么?
Example : You're implementing the MVP pattern, so you need a Presenter within your Activity. This Presenter can do a network operation to download items. When the device rotate, your Activity is being destroyed and recreated but you would like to keep the network operation going and just get back the pre-rotation presenter.
示例:您正在实施MVP模式,因此您需要在Activity中使用Presenter。此演示者可以执行网络操作来下载项目。当设备旋转时,您的Activity将被销毁并重新创建,但您希望保持网络运行,并且只需返回预旋转演示者。
Scoping the Component providing the Presenter with a custom PerActivity scope is the solution, so you have to keep the Component instance through this rotation to get injected the same instance of Presenter as the first launch of the Activity.
为提供Presenter的组件定义一个自定义PerActivity范围是解决方案,因此您必须通过此循环保持Component实例,以便在首次启动Activity时注入相同的Presenter实例。
How can we deal with this ? I thought of a kind of Component Cache (like a HashMap ?) that could be provided by an Application Component living inside the Application class.
我们怎么处理这个?我想到了一种组件缓存(如HashMap?),它可以由生活在Application类中的Application Component提供。
2 个解决方案