5

So I've been spending a lot of time with Mortar and Flow this weekend, and I think I've finally worked most of it out. I've found that it's a little bit more complex than I originally thought, mostly because I haven't quite gotten my mind around Dagger's ObjectGraph Scoping, which Mortar relies on heavily. I've read as much as I could find on this on Dagger's site, but I find information on this subject lacking when it relates specifically to Dagger.

So I have a few questions: 1. I see examples of them scoping @Singleton's:

@Layout(R.layout.screen_friend)
public class FriendScreen implements Blueprint {

  @Override public String getMortarScopeName() {
    return getClass().getName();
  }

  @Override public Object getDaggerModule() {
    return new Module();
  }

  @dagger.Module(
      injects = FriendView.class
  )
  static class Module {
  }

  @Singleton
  public static class Presenter extends ViewPresenter<TestView> {

    @Inject
    public Presenter() {
      Log.d("FriendScreen", "Friend Presenter Created");
    }

    @Override protected void onLoad(Bundle savedInstanceState) {
      super.onLoad(savedInstanceState);
    }
  }

Is Presenter in this case scoped to this Module specifically because it's an inner class? 2. How can I make sure that an instance of it is only created in this Object Graph but not the global application object graph? 2. What if the Presenter was too big, and I wanted to move it to it's own separate class? How would I scope it just to this module? 3. I noticed that some of the Module classes in their examples are static, and others aren't. Does this have any effect on scoping? 4. Where can I read more to better understand Dagger's Object Graph. I need to get a better understanding of includes, injects, addsTo and how those are used in ObjectGraph creation etc:

@dagger.Module( //
      includes = ActionBarModule.class,
      injects = MainView.class,
      addsTo = ApplicationModule.class, //
      library = true //
  )
spierce7
  • 14,797
  • 13
  • 65
  • 106

1 Answers1

0
  1. I don't believe presenters are scoped to a module since on a rotation they are preserved. the @Singleton annotation also leads me to believe that a presenter is on a global graph and just binds to a view when an activity recreates itself.

  2. Here's a good scoping source.

  3. Effective Java has a fantastic explanation about static inner vs non static inner vs anonymous classes if you want to learn more about those.

FriendlyMikhail
  • 2,857
  • 23
  • 39