gpt4 book ai didi

android - Dagger 2 基类注入(inject)

转载 作者:行者123 更新时间:2023-12-01 03:41:45 27 4
gpt4 key购买 nike

在 Dagger 1 中,我有一个基类设置,它可以处理创建范围图并将依赖项注入(inject)当前对象。例如...

public abstract class MyBaseActivity extends Activity {
private ObjectGraph graph;

protected void onCreate(Bundle savedInstanceState) {
graph = ((MyApp) getApplication()).plus(getModules());
graph.inject(this);
}

protected Object[] getModules();
}

public class MyClass extends MyBaseActivity {

@Inject SomeDep someDep;

@Override
protected Object[] getModules() {
return new Object[/* Contains a module that provides SomeDep */];
}
}

这允许每个子类在标准应用程序模块之外补充他们自己的模块集。

在玩过 Dagger 2 之后,似乎无法处理类似的情况......
public abstract class MyBaseActivity extends Activity {
private MyBaseActivityComponent component;

protected void onCreate(Bundle savedInstanceState) {
component = ((MyApp) getApplication()).component().plus(/* Can not accept an array */);
component.inject(this);
}
}

我通过修改 MyBaseActivityComponent 来解决上述问题,以便列出它可能使用的所有可能模块......
@Subcomponent(modules = {
Module1.class,
Module2.class
})
public interface MyBaseActivityComponent {
public void inject(MyBaseActivity activity);
}

所以现在我可以做这样的事情......
public abstract class MyBaseActivity extends Activity {
private MyBaseActivityComponent component;

protected void onCreate(Bundle savedInstanceState) {
component = ((MyApp) getApplication()).component().plus(new Module1(), new Module2());
component.inject(this);
}
}

但是现在我遇到了一个问题,注入(inject)将注入(inject) MyBaseActivity 的依赖项,而不是它的子类。建议?

最佳答案

从理论上讲,您可以这样做。

1.) 指定子范围

@Scope
@Retention(RUNTIME)
public @interface PerActivity {
}

2.) 指定父组件
@Singleton
@Component(modules={Module1.class, Module2.class)
public interface MyApplicationComponent {
Dependency1 providesDependency1();
Dependency2 providesDependency2();
}

3.) 指定子组件
@PerActivity
@Component(dependencies={MyApplicationComponent.class}, modules={Module3.class})
public interface MyBaseActivityComponent extends MyApplicationComponent {
void inject(BaseActivity baseActivity);

Dependency3 providesDependency3();
}

4.) 创建你的模块
@Module
public class Module3 {
@Provides
@PerActivity
public Dependency3 providesDependency3() {
return new Dependency3();
}
}

5.) 创建 Activity 级范围组件
public class BaseActivity extends AppCompatActivity {
private MyBaseActivityComponent baseComponent;

@Override
public void onCreate(Bundle saveState) {
super.onCreate(saveState);
baseComponent = DaggerBaseActivityComponent.builder()
.applicationComponent(((MyApp)getApplication()).component())
.build();
}

public MyBaseActivityComponent baseComponent() {
return baseComponent;
}

@Override
public void onDestroy() {
component = null;
super.onDestroy();
}
}

如果有效,请回复,之前我忘记在我的 Component 中指定依赖项并得到编译错误,但它应该像这样工作。

此外,如果您需要为每个 Activity 指定一个子组件,那么您可以在 BaseActivityComponent 中使用配置方法指定依赖项零件...
@PerActivity
@Component(dependencies={MyBaseActivityComponent.class}, modules={Module4.class})
public interface MyActivityComponent extends MyBaseActivityComponent {
public void inject(MyActivity myActivity);

Dependency4 providesDependency4();
}

@Module
public class Module4 {
@PerActivity
@Provides
public Dependency4 providesDependency4(Dependency3 dependency3) {
return new Dependency4(dependency3);
}
}

public class MyActivity extends MyBaseActivity {
private MyActivityComponent component;

@Override
public void onCreate(Bundle saveInstanceState) {
super.onCreate(saveInstanceState);
component = DaggerMyActivityComponent.builder()
.applicationComponent(((MyApp)getApplication()).component())
.myBaseActivityComponent(baseComponent())
.build();
}

@Override
public void onDestroy() {
component = null;
super.onDestroy();
}
}

编辑 : @Subcomponent根据 the docs 使用子组件工厂方法替换组件依赖项仅当您使用以下模式(也就是使用供应/工厂方法定义将子组件嵌入父组件中)时:
   @Singleton @Component
interface ApplicationComponent {
// component methods...

RequestComponent newRequestComponent(RequestModule requestModule);
}

在哪里
  @Subcomponent(modules={RequestModule.class})
interface RequestComponent {
RequestSomething requestSomething();
}

关于android - Dagger 2 基类注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30099747/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com