如何使用Dagger 2的多个依赖项?

时间:2017-07-31 07:51:13

标签: android dagger-2

我创建了NetComponent和NetModule,我在其中初始化了所有必需的用于网络通信的库。

这是 NetComponent 代码:

@Singleton
@Component(modules={AppModule.class, NetModule.class})
public interface NetComponent {
   Retrofit retrofit();
}

我已经在Application类中初始化了NetComponent。

public class App extends Application {

private NetComponent mNetComponent;

@Override
public void onCreate() {
    super.onCreate();

    mNetComponent = DaggerNetComponent.builder()
            .appModule(new AppModule(this))
            .netModule(new NetModule("mock.com"))
            .build();
}

public NetComponent getNetComponent() {
    return mNetComponent;
   }
}

现在我想在我的应用程序中使用SharedPreferences,所以我创建了SharedPreferencesModule和Component类。

@Module
public class SharedPreferenceModule {

Application mApplication;

public SharedPreferenceModule(Application mApplication) {
    this.mApplication = mApplication;
}

@Provides
@Singleton
Application providesApplication() {
    return mApplication;
}

@Provides
@Singleton
SharedPreferences providesSharedPreferences(Application application) {
    return PreferenceManager.getDefaultSharedPreferences(application);
   }
}

以下是SharedPreferencesComponent的代码:

@Singleton
@Component(modules = {SharedPreferenceModule.class})
public interface SharedPreferenceComponent {
    SharedPreferences getSharedPreference();
}

我想在 LoginAccountFragment 类中使用它。 LoginAccountFragment类拥有自己的模块和组件。

@Module
public class LoginAccountModule {

private LoginAccountView view;

public LoginAccountModule(LoginAccountView view){
    this.view = view;
}

@Provides
@FragmentScoped
LoginAccountView providesLoginAccountView(){
    return view;
}

@Provides
@FragmentScoped
LoginAccountPresenter providesLoginAccountPresenter(LoginAccountPresenterImpl presenter){
    return presenter;
    }
}

@FragmentScoped
@Component(dependencies = {NetComponent.class}, modules =   LoginAccountModule.class)
public interface LoginAccountComponent {
    void inject(LoginAccountFragment fragment);
}

如何在LoginAccountFragment中注入SharedPreferences?

1 个答案:

答案 0 :(得分:1)

@Singleton
@Component(modules={AppModule.class, NetModule.class, SharedPreferenceModule.class})
public interface ApplicationComponent {
   Retrofit retrofit();
   SharedPreferences sharedPreferences();
}


public class App extends Application {

    private ApplicationComponent applicationComponent;

    @Override
    public void onCreate() {
        super.onCreate();

        this.applicationComponent = DaggerNetComponent.builder()
                .appModule(new AppModule(this))
                .netModule(new NetModule("https://mock.com"))
                .build();
    }

    public ApplicationComponent applicationComponent() {
        return applicationComponent;
    }
}

@Module
public class SharedPreferenceModule {
    @Provides
    @Singleton
    SharedPreferences providesSharedPreferences(Application application) {
         return PreferenceManager.getDefaultSharedPreferences(application);
    } 
}


@Module
public class LoginAccountModule {

    private LoginAccountView view;

    public LoginAccountModule(LoginAccountView view){
        this.view = view;
    }

    @Provides
    LoginAccountView loginAccountView(){
        return view;
    }

    @Provides
    @FragmentScoped
    LoginAccountPresenter loginAccountPresenter(LoginAccountPresenterImpl presenter) {
        return presenter;
    }
}

@FragmentScoped
@Component(dependencies = {ApplicationComponent.class}, modules = LoginAccountModule.class)
public interface LoginAccountComponent extends ApplicationComponent {
    void inject(LoginAccountFragment fragment);
}