2017-08-11 7 views
0

Dagger2を理解しようとしています。私はいくつかの例を踏襲し、1つのコンポーネント/モジュールを使用することは理にかなっていますが、もう1つ追加するとわかりやすくなります。アプリケーションクラスで複数のコンポーネントを使用することはできませんか?アプリケーションクラスのDaggerコンポーネントを2つ使用する

両方ダガーコンポーネントは、「...のシンボルを解決できません」

public class MyApplication extends Application { 

StorageComponent component; 
ImageDownloaderComponent imageDownloaderComponent; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    component = DaggerStorageComponent 
      .builder() 
      .storageModule(new StorageModule(this)) 
      .build(); 

    imageDownloaderComponent = DaggerImageDownloaderComponent 
      .builder() 
      .imageDownloaderModule(new ImageDownloaderModule(this)) 
      .build(); 

} 

public StorageComponent getComponent() { 
    return component; 
} 

public ImageDownloaderComponent getImageDownloaderComponent() { 
    return this.imageDownloaderComponent; 
} 
} 
+1

あなたは私のアプリケーションクラスで複数のコンポーネントを使用することができます。 return imageDownloaderComponentを変更しようとしました。プロジェクトを再ビルドします。 – eurosecom

答えて

0

を赤く強調表示し、言っている私は1つのモジュールで両方の作品(SharedPrefsとピカソ)を入れてしまったと私のアプリケーションクラスのコンポーネントを返しました。言葉が意味をなさないことを願っています。

@Module 
public class StorageModule { 
private final MyApplication application; 

public StorageModule(MyApplication application) { 
    this.application = application; 
} 

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

@Singleton 
@Provides 
ImageDownloader provideImageDownloader() { 
    return new ImageDownloader(application); 
} 
} 

MyApplicationをクラス:

public class MyApplication extends Application { 

StorageComponent component; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    component = DaggerStorageComponent 
      .builder() 
      .storageModule(new StorageModule(this)) 
      .build(); 

} 

public StorageComponent getComponent() { 
    return component; 
} 
} 
関連する問題