title: Adding Non Activity Classes to Dagger 2 Graph Android
body: <p>I'm having a hard time wrapping my head around how to use Dagger 2.0 outside of the limited examples I've seen. Let's take an example reading application. In this reading app, there is a library of a user's stories and the ability to Log in. The classes of interest for the purpose of this example are:</p>
  
  <p><code>MainApplication.java</code> - extends Application</p>
  
  <p><code>LibraryManager.java</code> - Manager which is responsible for adding/removing stories in the user's library. This is called from the <code>MainApplication</code> </p>
  
  <p><code>AccountManager.java</code> - Manager which is responsible for saving all a user's login information. It can be called from the LibraryManager</p>
  
  <p>I'm still trying to wrap my head around what Components and Modules I should be creating. Here's what I can gather so far:</p>
  
  <p>Create a <code>HelperModule</code> that provides an <code>AccountManager</code> and <code>LibraryManager</code> instance:</p>
  
  <pre><code>@Module
  public class HelperModule {
  
      @Provides
      @Singleton
      AccountManager provideAccountManager() {
          return new AccountManager();
      }
  
      @Provides
      @Singleton
      LibraryManager provideLibraryManager() {
          return new LibraryManager();
      }
  
  }
  </code></pre>
  
  <p>Create a <code>MainApplicationComponent</code> that lists the <code>HelperModule</code> in its list of modules:</p>
  
  <pre><code>@Singleton
  @Component(modules = {AppModule.class, HelperModule.class})
  public interface MainApplicationComponent {
      MainApplication injectApplication(MainApplication application);
  }
  </code></pre>
  
  <p>Include <code>@Injects LibraryManager libraryManager</code> in the <code>MainApplication</code> and inject the application into the graph. Finally it queries the injected <code>LibraryManager</code> for the number of stories in the library:</p>
  
  <pre><code>public class MainApplication extends Application {
  
      @Inject LibraryManager libraryManager;
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          component = DaggerMainApplicationComponent.builder()
                  .appModule(new AppModule(this))
                  .helperModule(new HelperModule())
                  .build();
          component.injectApplication(this);
  
          // Now that we have an injected LibraryManager instance, use it
          libraryManager.getLibrary();
      }
  }
  </code></pre>
  
  <p>Inject the <code>AccountManager</code> into the <code>LibraryManager</code> </p>
  
  <pre><code>public class LibraryManager {
      @Inject AccountManager accountManager;
  
      public int getNumStoriesInLibrary() {
          String username = accountManager.getLoggedInUserName();
          ...
      }
  }
  </code></pre>
  
  <p>However the problem is that the <code>AccountManager</code> is null when I try to use it in the <code>LibraryManager</code> and I don't understand why or how to solve the problem. I'm thinking that it's because the <code>MainApplication</code> that was injected into the graph doesn't use the AccountManager directly, but then do I need to inject the <code>LibraryManager</code> into the graph some how? </p>