Application Component - nhaarman/Triad GitHub Wiki

A Presenter may have dependencies that need to be passed into its constructor. The application component is a class which provides these dependencies. This class is completely defined yourself, and is the same for all your screens.

For example, a common dependency is some repository instance:

public class ApplicationComponent {

  @NonNull
  private final MyRepository mMyRepository;

  public MainComponent(@NonNull final MyRepository myRepository) {
    mMyRepository = myRepository;
  }

  public MyRepository myRepository() {
    return mMyRepository;
  }
}

Your Application class is responsible for creating this component, like so:

public class MyApplication extends TriadAppliation<ApplicationComponent> {

  @Nullable
  private ApplicationComponent mApplicationComponent;

  @NonNull
  @Override
  public ApplicationComponent getApplicationComponent() {
    if (mApplicationComponent == null) {
      mApplicationComponent = new ApplicationComponent(new MyRepository());
    }

    return mApplicationComponent;
  }
}

You can call Screen.applicationComponent() in the createPresenter(...) method to create a Presenter instance:

public class MyScreen extends Screen<ApplicationComponent> {

  @NonNull
  @Override
  protected Presenter<?, ?> createPresenter(@NonNull final Class<? extends Presenter<?,?>> presenterClass) {
    return new MyPresenter(activityComponent().myRepository());
  }
}

You can add any application wide dependency to your application component, or even use Dagger to inject your dependencies.

⚠️ **GitHub.com Fallback** ⚠️