我正在尝试使用Dagger 2
来注入我的应用程序类MyApplication
,因为我在不同的地方使用它。这是我使用Dagger 2.11
MyApplication.java
public class MyApplication extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
AppInjector.init(this);
}
@Override
public DispatchingAndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
}
AppInjector.java
public class AppInjector {
public static void init(MyApplication application){
//Initialize dagger and inject the application
DaggerAppComponent.builder().application(application).build().inject(application);
application.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle aBundle) {
handleActivity(activity);
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle aBundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
});
}
private static void handleActivity(Activity activity){
if(activity instanceof HasSupportFragmentInjector ){
AndroidInjection.inject(activity);
}
if (activity instanceof FragmentActivity){
((FragmentActivity) activity).getSupportFragmentManager()
.registerFragmentLifecycleCallbacks(
new FragmentManager.FragmentLifecycleCallbacks() {
@Override
public void onFragmentCreated(FragmentManager fm, Fragment f,
Bundle savedInstanceState) {
if (f instanceof Injectable) {
Log.i("LifecycleCallbacks", "injected:" + f);
AndroidSupportInjection.inject(f);
}
}
}, true);
}
}
AppComponent.java
@Singleton
@Component(modules = {
AndroidInjectionModule.class,
ActivityBuilder.class,
AppModule.class
})
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance Builder application(Application application);
AppComponent build();
}
void inject(MyApplication application);
}
但是,每次我尝试在构造函数中使用@Inject MyApplication application
时,dagger会抛出一个错误,如果没有@Provides
更多信息,我不确定应该在任何地方使用应用程序,而不仅仅是Context
?如果是这样,我将如何提供Context
?
答案 0 :(得分:3)
看看你的Builder ......
@Component.Builder
interface Builder {
--> @BindsInstance Builder application(Application application);
AppComponent build();
}
所有Dagger都知道你的Application
,你从不提及MyApplication
,因此注入它会失败。
我不知道为什么你必须专门注入MyApplication
,但最简单的解决办法是将其改为绑定你的MyApplication
而不是......
@Component.Builder
interface Builder {
@BindsInstance Builder application(/** --> */ MyApplication application);
AppComponent build();
}
然后Dagger知道MyApplication
而不是Application
。要解决这个问题,你可以添加一个绑定其他类型的模块,这很容易,因为你有子类型......例如。
@Module interface AppModule { // could also be an abstract class
@Binds Application bindApplication(MyApplication application);
// if you also want to bind context
@Binds Context bindContext(MyApplication application);
}
只需将此模块添加到您的组件中即可。