{ @Inject B b; @Inject C c; } public class B extends Fragment { @Inject D d; @Inject E e; } public class C extends Fragment { @Inject F f; } public class E extends View { @Inject G g; } A D B C E F G
class A extends Activity { @Inject B b; @Inject C c; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Scope scope = Toothpick.openScope(this); Toothpick.inject(this, scope); ... } INJECTING STUFF: GRAPH AND ROOT
CREATE 2. INJECT FACTORIES & MEMBER INJECTORS class B$$Factory implements Factory { public B createInstance(Scope scope) { scope = getTargetScope(scope); B b = new B(); memberInjector.inject(b, scope); return b; } } class A$$MemberInjector implements MI { public A inject(A a, Scope scope) { a.b = scope.getInstance(B.class); a.c = scope.getInstance(C.class); } }
CREATE 2. INJECT FACTORIES & MEMBER INJECTORS B C class B$$Factory implements Factory { public B createInstance(Scope scope) { scope = getTargetScope(scope); B b = new B(); memberInjector.inject(b, scope); return b; } } class A$$MemberInjector implements MI { public A inject(A a, Scope scope) { a.b = scope.getInstance(B.class); a.c = scope.getInstance(C.class); } }
CREATE 2. INJECT FACTORIES & MEMBER INJECTORS B C class B$$Factory implements Factory { public B createInstance(Scope scope) { scope = getTargetScope(scope); B b = new B(); memberInjector.inject(b, scope); return b; } } class A$$MemberInjector implements MI { public A inject(A a, Scope scope) { a.b = scope.getInstance(B.class); a.c = scope.getInstance(C.class); } }
CREATE 2. INJECT FACTORIES & MEMBER INJECTORS B C D E F class B$$Factory implements Factory { public B createInstance(Scope scope) { scope = getTargetScope(scope); B b = new B(); memberInjector.inject(b, scope); return b; } } class A$$MemberInjector implements MI { public A inject(A a, Scope scope) { a.b = scope.getInstance(B.class); a.c = scope.getInstance(C.class); } }
CREATE 2. INJECT FACTORIES & MEMBER INJECTORS B C D E F class B$$Factory implements Factory { public B createInstance(Scope scope) { scope = getTargetScope(scope); B b = new B(); memberInjector.inject(b, scope); return b; } } class A$$MemberInjector implements MI { public A inject(A a, Scope scope) { a.b = scope.getInstance(B.class); a.c = scope.getInstance(C.class); } }
CREATE 2. INJECT FACTORIES & MEMBER INJECTORS B C D E F G class B$$Factory implements Factory { public B createInstance(Scope scope) { scope = getTargetScope(scope); B b = new B(); memberInjector.inject(b, scope); return b; } } class A$$MemberInjector implements MI { public A inject(A a, Scope scope) { a.b = scope.getInstance(B.class); a.c = scope.getInstance(C.class); } }
CREATE 2. INJECT FACTORIES & MEMBER INJECTORS B C D E F G class B$$Factory implements Factory { public B createInstance(Scope scope) { scope = getTargetScope(scope); B b = new B(); memberInjector.inject(b, scope); return b; } } class A$$MemberInjector implements MI { public A inject(A a, Scope scope) { a.b = scope.getInstance(B.class); a.c = scope.getInstance(C.class); } }
class A extends Activity { @Inject B b; @Inject C c; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Scope scope = Toothpick.openScope(this); Toothpick.inject(this, scope); ... } FACTORIES & MEMBER INJECTORS
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Context context = getContext(); if (!context instanceof Activity) { throw new ThisIsBadException(); } ... } CONTEXT CAN BE DIFFERENT FROM ACTIVITY: CONTEXTWRAPPER
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Context context = getContext(); if (!context instanceof Activity) { throw new ThisIsBadException(); } ... } Crash CONTEXT CAN BE DIFFERENT FROM ACTIVITY: CONTEXTWRAPPER
scope) { scope = getTargetScope(scope); B b = new B(); memberInjector.inject(b, scope); return b; } } class B$$MemberInjector implements MI { public B inject(B b, Scope scope) { b.d = scope.getInstance(D.class); b.e = scope.getInstance(E.class); } } public class B extends Fragment { @Inject D d; @Inject E e; private B(F f) { Toothpick.inject(this, scope); } }
scope) { scope = getTargetScope(scope); B b = new B(); memberInjector.inject(b, scope); return b; } } class B$$MemberInjector implements MI { public B inject(B b, Scope scope) { b.d = scope.getInstance(D.class); b.e = scope.getInstance(E.class); } } X public class B extends Fragment { @Inject D d; @Inject E e; private B(F f) { Toothpick.inject(this, scope); } }
D d; @Inject E e; private B(F f) { Toothpick.inject(this, scope); } } class B$$MemberInjector implements MI { public B inject(B b, Scope scope) { b.d = scope.getInstance(D.class); b.e = scope.getInstance(E.class); } } D B E DI root
d; @Inject E e; public B() { Toothpick.inject(this, scope); } } MOCK ALL YOUR DEPENDENCIES public class BTest extends EasyMockSupport { @Rule public EasyMockRule easyMockRule; @Rule public ToothpickRule toothpickRule; @Mock D d; @Mock E e; }
2. DO NOT CALL LAZY.GET() AFTER ONDESTROY IF INJECTED USING ACTIVITY SCOPE 3. DO NOT OPEN SCOPE USING CONTEXT: CONTEXTSCOPEFINDER 4. USE SUPPRESSWARNING FOR DI ROOTS 5. MOCK ALL YOUR DEPENDENCIES