Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 注入类如何获得对注入器的引用?_Android_Dagger 2 - Fatal编程技术网

Android 注入类如何获得对注入器的引用?

Android 注入类如何获得对注入器的引用?,android,dagger-2,Android,Dagger 2,我最近开始在一个小型Android项目中使用Dagger2。我不确定我应该在哪里构建我的@组件 假设我有一个@模块,该模块提供的依赖关系反过来又依赖于应用程序。显然,您无法实例化@模块,因此无法在不引用应用程序的情况下构建@组件。在这种情况下,应用程序本身构建并保存对@组件的引用是否有意义,然后可以获取哪些活动和片段来注入它们自己?换句话说,与此相反: MyComponent component = DaggerMyComponent.builder() .myModule(new My

我最近开始在一个小型Android项目中使用Dagger2。我不确定我应该在哪里构建我的
@组件

假设我有一个
@模块
,该模块提供的依赖关系反过来又依赖于
应用程序
。显然,您无法实例化
@模块
,因此无法在不引用
应用程序的情况下构建
@组件
。在这种情况下,
应用程序
本身构建并保存对
@组件的引用是否有意义,然后可以获取哪些活动和片段来注入它们自己?换句话说,与此相反:

MyComponent component = DaggerMyComponent.builder()
    .myModule(new MyModule((MyApp) getApplication()))
    .build();
component.inject(this);
活动就是这样做的:

((MyApp) getApplication()).getMyComponent().inject(this);
第二种方法有什么缺点吗?如果模块提供了
@Singleton
依赖项,是否有必要使用第二种方法

编辑:我编写了一个非Android测试程序。正如我所期望的那样,
@组件
接口的不同实例产生了
@Singleton
资源的不同实例。因此,我最后一个问题的答案似乎是肯定的,除非
@Component
本身是一个单体,存在其他机制

final AppComponent component1 = DaggerAppComponent.create();
final AppComponent component2 = DaggerAppComponent.create();
System.out.println("same AppComponent: " + component1.equals(component2)); // false
// the Bar producer is annotated @Singleton
System.out.println("same component, same Bar: " + component1.bar().equals(component1.bar())); // true
System.out.println("different component, same Bar: " + component1.bar().equals(component2.bar())); // false

组件必须位于接口中。假设您有这样一个模块

@Module
public class MainActivityModule {

    @Provides
    public Gson getGson(){
        return new Gson();
    }
}
现在,您需要为该模块创建一个接口,以便在活动中使用它。我将活动注入到这个接口中,但是当您想要用于许多其他活动时,这将是一个棘手的问题,所以现在让我们只说您想要使用MainActivity

@Component(
    modules = MainActivityModule.class) //The module you created 
public interface IAppModule {
    void inject(MainActivity activity);
}
现在您可以在MainActivity中使用,但首先要构建项目,因为Dagger2需要使自己的类依赖于您所创建的模块和组件。请注意,您尚未创建类
DaggerIAppModule
,它是在生成项目后创建的

IAppModule appComponent;

@Inject
Gson gson;

public void setupDaggerGraph(){ //call this method in your onCreate()
    appComponent = DaggerIAppModule.builder()
            .mainActivityModule(new MainActivityModule())
            .build();
    appComponent.inject(this);
}

你的建议是正确的。
@Singleton
组件在其生命周期内只保证一个
@Singleton
范围内的实例,因此您的应用程序必须保留该组件。

示例中的
@Module
不依赖于
应用程序
。它在接口IAppModule中的
@component
中使用?我不太清楚你的意思…至于你编辑的文章,你不能将任何活动注入到同一个组件中,你必须为每个活动创建一个组件,如果你对每个活动使用相同的模块,你可以将其作为依赖项使用,以便使用相同的单例@kevinkrumwied不是
newmymodule()
,而是
newmymodule(getApplication())
。在这种情况下,我想问的是,让应用程序实例化
MyModule
并为接口提供一个getter是否有意义。我不确定“必须为每个活动创建一个组件”是什么意思。我没有在活动中尝试过,但是我的测试应用程序使用相同的组件实例来注入它的所有片段,即,
((MyApp)getActivity().getApplication()).getMyComponent().inject(this)
我找到了一个支持这一点的例子:“我们应该在
应用程序
类中完成所有这些工作,因为在应用程序的整个生命周期中,这些实例只应该声明一次。”