Dagger 2注射不起作用

时间:2017-03-09 05:48:01

标签: android retrofit2 dagger-2

提供Gson,Retrofit和OkHttpClient单身人士的模块

@Module
public class MyModule {

    @Provides
    @Singleton
    Gson provideGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        return gsonBuilder.create();
    }

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient() {
        OkHttpClient client = new OkHttpClient();
        return client;
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl(BuildConfig.SERVER_BASE_URL)
                .client(okHttpClient)
                .build();
        return retrofit;
    }
}

允许将单身人士注入活动和片段的组件

@Singleton
@Component(modules={MyModule.class})
public interface MyComponent {

    void inject(Activity activity);
    void inject(Fragment fragment);
    void inject(Application application);
}

构建组件的主要应用程序类

public class MyApp extends Application{


    private MyComponent component;

    @Inject
    Retrofit retrofit;

    @Override
    public void onCreate() {
        super.onCreate();
        component= DaggerMyComponent.builder()
                .myModule(new MyModule()).build();
        getComponent().inject(this); // inject retrofit here
    }

    public MyComponent getComponent() {
        return component;
    }
}

这是片段,我试图注入Retrofit。

public class MyFragment extends Fragment {

    @Inject
    Retrofit retrofit;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        ((MyApp)getActivity().getApplication()).getComponent().inject(this);
      ....

    }
}

在MyApp和MyFragment中,改造实例都为空。

1 个答案:

答案 0 :(得分:2)

您可以在同一个组件中注入Activity,Fragment和Application。您需要为每个Activity,Fragment和Component创建Separate组件,如下所示:

<强> Activiy

在您的所有活动中使用此组件:

@Singleton
@Component(modules={MyModule.class})
public interface MyActivityComponent {
    void inject(Activity activity);
    void inject(AnotherActivity activity);
}

注入这样的活动:

component= DaggerMyActivityComponent.builder()
                .myModule(new MyModule()).build();
        getComponent().inject(this)

<强>片段

在所有片段中使用此组件:

    @Singleton
    @Component(modules={MyModule.class})
    public interface MyFragmentComponent {

        void inject(Fragment fragment);
        void inject(AnotherFragmen fragment);
    }

像这样注入片段:

component= DaggerMyFragmentComponent.builder()
                .myModule(new MyModule()).build();
        getComponent().inject(this)

<强>应用

在您的应用程序中使用此组件:

    @Singleton
    @Component(modules={MyModule.class})
    public interface MyAppComponent {
        void inject(Application application);
    }

在这样的应用程序中注入:

component= DaggerMyAppComponent.builder()
                .myModule(new MyModule()).build();
        getComponent().inject(this)