【问题标题】:Dagger 2 injection not working匕首2注射不起作用
【发布时间】:2017-03-09 05:48:00
【问题描述】:

提供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 中,改造实例均为空。

【问题讨论】:

  • 我觉得不错。
  • 我也是这么想的,但它没有注入 Retrofit :(
  • @Coder 找到解决方案了吗?

标签: android retrofit2 dagger-2


【解决方案1】:

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

活动

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

@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)

【讨论】:

  • 这行得通。那么有什么方法可以在不创建 Fragment 特定的 Inject 方法的情况下注入所有 Fragments 中的依赖项?
  • 我也遇到过这个问题,但目前我没有任何解决方案。
  • 代替inject(Activity activity) 试试doing inject(MyActivity activity)
猜你喜欢
  • 2015-11-04
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多