- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
假设已经说过here ,开发人员有责任保留组件实例以实现他们自己的作用域逻辑(因为作用域方法将为给定组件返回相同的实例)。
在整个 Activity 生命周期中保持此组件引用的简洁方法是什么?
示例:您正在实现 MVP 模式,因此您的 Activity 中需要一个 Presenter。此 Presenter 可以执行网络操作以下载项目。当设备旋转时,您的 Activity 将被销毁并重新创建,但您希望继续进行网络操作并只取回旋转前的演示者。
为 Presenter 提供自定义 PerActivity 范围的 Component 范围是解决方案,因此您必须通过此轮换保持 Component 实例,以便注入(inject)与第一次启动 Activity 相同的 Presenter 实例。
我们该如何应对?我想到了一种组件缓存(如 HashMap ?),它可以由应用程序类中的应用程序组件提供。
最佳答案
可以看到ribot/android-boilerplate的实现展示应用程序。他们选择的解决方案是拥有一个 static Map<Long, ConfigPersistentComponent>
在 BaseActivity
里面,所有 Activity 都从中延伸。
public class BaseActivity extends AppCompatActivity {
private static final AtomicLong NEXT_ID = new AtomicLong(0);
private static final Map<Long, ConfigPersistentComponent> sComponentsMap = new HashMap<>();
private ActivityComponent mActivityComponent;
private long mActivityId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the ActivityComponent and reuses cached ConfigPersistentComponent if this is
// being called after a configuration change.
mActivityId = savedInstanceState != null ?
savedInstanceState.getLong(KEY_ACTIVITY_ID) : NEXT_ID.getAndIncrement();
ConfigPersistentComponent configPersistentComponent;
if (!sComponentsMap.containsKey(mActivityId)) {
// Creating new component
configPersistentComponent = DaggerConfigPersistentComponent.builder()
.applicationComponent(BoilerplateApplication.get(this).getComponent())
.build();
sComponentsMap.put(mActivityId, configPersistentComponent);
} else {
// Reusing component
configPersistentComponent = sComponentsMap.get(mActivityId);
}
mActivityComponent = configPersistentComponent.activityComponent(new ActivityModule(this));
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(KEY_ACTIVITY_ID, mActivityId);
}
@Override
protected void onDestroy() {
if (!isChangingConfigurations()) {
// Activity is finishing, removing the component
sComponentsMap.remove(mActivityId);
}
super.onDestroy();
}
...
}
关于java - 在方向改变时处理 Dagger 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38766352/
Dagger 和 Dagger 2.0 有什么区别,为什么 Google 决定 fork 现有项目? 最佳答案 来自 Dagger 2 presentation 的一些引述 Dagger 1 的问题:
我想制作一个包含一个主要 Activity 和多个 fragment 的简单项目。在这里,我在一项 Activity 中有两个 fragment ,我想将演示者注入(inject)登录 fragmen
我是 dagger 的新手,我正在寻找如何在 dagger-2.x 中实现 spring 配置文件等功能。我想为我的开发环境和生产环境使用不同的 bean,但我使用的是带有 Java 的 Dagger
假设我有两个服务 AService 和 BService,它们都需要一个 API key 。 所以在我的模块中,我不能做类似的事情: @Provides @Singleton @A @ApiKey S
我对 Dagger 很陌生——我什至不知道它是否适用于我的应用程序 我有一个搜索页面,它返回有关给定名人的最新消息。 我写了一个测试来验证当我们搜索一个受欢迎的名人时结果会出现在页面上。 page有一
我最近将 dagger 2.8 更新为 2.9 dagger。和最新版本的文档已添加如下: -添加@BindsInstance组件构建器可以轻松绑定(bind)在图之外构建的实例。 -制作人:已添加
Dagger documentation页面说: To get the most out of compile-time validation, create a module that includ
我可以知道之间的区别吗? @Singleton VS 静态 在dagger2中提供? @Provides static User currentUser(AuthManager authManager
我有一个功能,我需要为不同的用户提供不同的房间数据库。 我正在使用 Dagger 2 创建房间数据库。我的应用程序组件创建一个房间数据库。当用户切换到另一个用户时,我想为此创建新的房间数据库,我需要创
我是 Dagger 2 的新手,正在尝试 IntelliJ 中的 Dagger 2 Coffee Example,它似乎不会生成 DaggerCoffeeApp_Coffee(它应该生成它),即使我密
我正在尝试在 Android 上使用 Dagger 2。我以前让它工作,我有一个 appModule 将依赖项注入(inject)应用程序中的特定类。我的问题是我收到错误 Error:(14, 55)
我是 kotlin 和 Dagger 的新手。我有一个小问题,我不知道如何解决,也没有找到解决方案。 这就是我所拥有的: @Module class AppModule (app: Applicati
我正在尝试使用 Dagger 建立一个项目。现在,当我构建时,没有为组件生成任何 Dagger* 类,并且出现以下错误: Error:(10, 8) error: [com.redditapp.dag
在 Dagger 中,我有时会看到有些组件只扩展一个接口(interface),而其他组件则使用 dependencies。 例如我们有一个基础组件: @Singleton @Component(mo
有没有办法告诉 Dagger 2 如何提供一些东西,但不允许它被注入(inject)? 假设我要注入(inject)一个 Qux .一个 Qux需要 Foo和 Bar : @Provides @Sin
( x-post from /r/androiddev ) 我只想在序言中说这不是一篇“哪个更好”的帖子;这严格来说是一个关于如何使用 Dagger 构建某些东西的问题(以及如何在 Kodein 中构
https://developer.android.com/topic/libraries/architecture/ 在Android架构蓝图中,为什么dagger是基于mvp架构而不是MVVM架构
我第一次开始同时使用 Kotlin 和 Dagger 2。我假设一切都与 Java 中的相同,但显然不完全相同。 Dagger 不会为我生成 Dagger* 文件。这是我的代码: 组件: @PerAc
我正在尝试将 Dagger 2 与 eclipse 集成,我使用的库如下: dagger-2.0.jar dagger-compiler-2.0.jar guava-13.0.1.jar javawr
我想练习这个Dagger 2 Vehicle Motor例子。 除了我的 gradel.build 之外,我所做的一切都与该教程中的一样: compile 'com.google.dagger:dag
我是一名优秀的程序员,十分优秀!