├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── DaggerTest.iml ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── opticus │ │ │ └── daggertest │ │ │ ├── App.java │ │ │ ├── BaseActivity.java │ │ │ ├── BaseFragment.java │ │ │ ├── FirstFragment.java │ │ │ ├── MainActivity.java │ │ │ ├── SecondActivity.java │ │ │ ├── SecondFragment.java │ │ │ ├── SelectActivity.java │ │ │ ├── async │ │ │ ├── AsyncComponent.java │ │ │ ├── AsyncInjectActivity.java │ │ │ └── AsyncModule.java │ │ │ ├── di │ │ │ ├── ActModule.java │ │ │ ├── AppComponent.java │ │ │ ├── AppModule.java │ │ │ ├── BusModule.java │ │ │ ├── DbModule.java │ │ │ ├── FirstFrComponent.java │ │ │ ├── HasComponent.java │ │ │ ├── MainActComponent.java │ │ │ ├── ManagerBModule.java │ │ │ ├── SecondActComponent.java │ │ │ ├── SecondActModule.java │ │ │ ├── SecondFrComponent.java │ │ │ └── scope │ │ │ │ ├── PerActivity.java │ │ │ │ ├── PerApplication.java │ │ │ │ └── PerFragment.java │ │ │ └── managers │ │ │ ├── DbHelper.java │ │ │ ├── HugeManager.java │ │ │ ├── ManagerA.java │ │ │ └── ManagerB.java │ └── res │ │ ├── layout │ │ ├── activity_async_inject.xml │ │ ├── activity_main.xml │ │ ├── activity_main2.xml │ │ ├── activity_second.xml │ │ ├── activity_select.xml │ │ ├── content_main.xml │ │ ├── fragment_blank.xml │ │ ├── fragment_main.xml │ │ └── fragment_second.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── opticus │ └── daggertest │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | /captures 8 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | DaggerTest -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 22 | 23 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /DaggerTest.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 39 | 210 | 223 | 224 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 118 | 289 | 302 | 303 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | mavenCentral() 4 | } 5 | dependencies { 6 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 7 | } 8 | } 9 | 10 | apply plugin: 'com.android.application' 11 | apply plugin: 'com.neenbedankt.android-apt' 12 | 13 | android { 14 | compileSdkVersion 23 15 | buildToolsVersion "23.0.2" 16 | 17 | defaultConfig { 18 | applicationId "com.opticus.daggertest" 19 | minSdkVersion 16 20 | targetSdkVersion 23 21 | versionCode 1 22 | versionName "1.0" 23 | } 24 | buildTypes { 25 | release { 26 | minifyEnabled false 27 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 28 | } 29 | } 30 | packagingOptions { 31 | exclude 'META-INF/services/javax.annotation.processing.Processor' 32 | } 33 | lintOptions { 34 | abortOnError false 35 | } 36 | } 37 | 38 | dependencies { 39 | compile 'com.android.support:appcompat-v7:23.1.1' 40 | compile 'com.android.support:design:23.1.1' 41 | compile 'com.google.dagger:dagger:2.0.1' 42 | compile 'com.google.dagger:dagger-producers:2.0-beta' 43 | apt 'com.google.dagger:dagger-compiler:2.0.1' 44 | compile 'com.squareup:otto:1.3.6' 45 | provided 'org.glassfish:javax.annotation:10.0-b28' 46 | compile 'io.reactivex:rxjava:1.0.10' 47 | } 48 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/opticus/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/App.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest; 2 | 3 | import android.app.Application; 4 | import android.util.Log; 5 | 6 | import com.opticus.daggertest.di.AppComponent; 7 | import com.opticus.daggertest.di.AppModule; 8 | import com.opticus.daggertest.di.DaggerAppComponent; 9 | import com.opticus.daggertest.di.HasComponent; 10 | import com.opticus.daggertest.managers.DbHelper; 11 | 12 | import javax.inject.Inject; 13 | 14 | public class App extends Application implements HasComponent { 15 | @Inject 16 | DbHelper dbHelper; 17 | private AppComponent component; 18 | 19 | @Override 20 | public void onCreate() { 21 | super.onCreate(); 22 | 23 | this.component = createComponent(); 24 | component.inject(this); 25 | Log.i("GTAG", "app db: " + dbHelper.hashCode()); 26 | } 27 | 28 | @Override 29 | public AppComponent getComponent() { 30 | return component; 31 | } 32 | 33 | public AppComponent createComponent() { 34 | return DaggerAppComponent.builder() 35 | .appModule(new AppModule(this)) 36 | .build(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | 6 | import com.opticus.daggertest.di.AppComponent; 7 | import com.squareup.otto.Bus; 8 | 9 | import javax.inject.Inject; 10 | 11 | public abstract class BaseActivity extends AppCompatActivity { 12 | 13 | @Inject 14 | protected Bus bus; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | initDiComponent(); 19 | super.onCreate(savedInstanceState); 20 | } 21 | 22 | abstract protected void initDiComponent(); 23 | 24 | protected AppComponent getAppComponent() { 25 | return ((App) getApplication()).getComponent(); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/BaseFragment.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.support.annotation.Nullable; 6 | import android.support.v4.app.Fragment; 7 | 8 | import com.opticus.daggertest.di.HasComponent; 9 | import com.squareup.otto.Bus; 10 | 11 | import javax.inject.Inject; 12 | 13 | public abstract class BaseFragment extends Fragment { 14 | @Inject 15 | Bus bus; 16 | 17 | @Override 18 | public void onActivityCreated(@Nullable Bundle savedInstanceState) { 19 | super.onActivityCreated(savedInstanceState); 20 | initDiComponent(); 21 | } 22 | 23 | abstract protected void initDiComponent(); 24 | 25 | @Override 26 | public void onResume() { 27 | super.onResume(); 28 | bus.register(this); 29 | } 30 | 31 | @Override 32 | public void onPause() { 33 | super.onPause(); 34 | bus.unregister(this); 35 | } 36 | 37 | public T getActComponent(Class clazz) { 38 | Activity activity = getActivity(); 39 | HasComponent has = (HasComponent) activity; 40 | return has.getComponent(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/FirstFragment.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | import com.opticus.daggertest.di.DaggerFirstFrComponent; 10 | import com.opticus.daggertest.di.FirstFrComponent; 11 | import com.opticus.daggertest.managers.ManagerA; 12 | 13 | import javax.inject.Inject; 14 | import javax.inject.Provider; 15 | 16 | public class FirstFragment extends BaseFragment { 17 | 18 | @Inject 19 | Provider managerA; 20 | 21 | public FirstFragment() { 22 | } 23 | 24 | @Override 25 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 26 | Bundle savedInstanceState) { 27 | return inflater.inflate(R.layout.fragment_main, container, false); 28 | } 29 | 30 | @Override 31 | public void onActivityCreated(Bundle savedInstanceState) { 32 | super.onActivityCreated(savedInstanceState); 33 | Log.i("GTAG", "1 fr bus: " + bus.hashCode()); 34 | Log.i("GTAG", "1 fr managerA: " + managerA.get().hashCode()); 35 | Log.i("GTAG", "1 fr managerA: " + managerA.get().hashCode()); 36 | Log.i("GTAG", "1 fr managerA: " + managerA.get().hashCode()); 37 | } 38 | 39 | @Override 40 | protected void initDiComponent() { 41 | FirstFrComponent.HasFirstFrDepends actComponent = getActComponent(FirstFrComponent.HasFirstFrDepends.class); 42 | DaggerFirstFrComponent.builder() 43 | .hasFirstFrDepends(actComponent) 44 | .build() 45 | .inject(this); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | 6 | import com.opticus.daggertest.di.ActModule; 7 | import com.opticus.daggertest.di.DaggerMainActComponent; 8 | import com.opticus.daggertest.di.HasComponent; 9 | import com.opticus.daggertest.di.MainActComponent; 10 | import com.opticus.daggertest.managers.ManagerA; 11 | import com.squareup.otto.Bus; 12 | 13 | import javax.inject.Inject; 14 | 15 | import dagger.Lazy; 16 | 17 | public class MainActivity extends BaseActivity implements HasComponent { 18 | 19 | MainActComponent actComponent; 20 | 21 | @Inject 22 | Lazy managerA; 23 | 24 | @Override 25 | public void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_main); 28 | Log.i("GTAG", "mAct bus: " + bus.hashCode()); 29 | Log.i("GTAG", "mAct managerA: " + managerA.get().hashCode()); 30 | Log.i("GTAG", "mAct managerA: " + managerA.get().hashCode()); 31 | Log.i("GTAG", "mAct managerA: " + managerA.get().hashCode()); 32 | } 33 | 34 | @Override 35 | protected void initDiComponent() { 36 | actComponent = DaggerMainActComponent 37 | .builder() 38 | .appComponent(getAppComponent()) 39 | .actModule(new ActModule(this)) 40 | .build(); 41 | actComponent.inject(this); 42 | } 43 | 44 | @Override 45 | public MainActComponent getComponent() { 46 | return actComponent; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/SecondActivity.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | 6 | import com.opticus.daggertest.di.DaggerSecondActComponent; 7 | import com.opticus.daggertest.di.HasComponent; 8 | import com.opticus.daggertest.di.SecondActComponent; 9 | import com.opticus.daggertest.di.SecondActModule; 10 | import com.squareup.otto.Bus; 11 | 12 | import javax.inject.Inject; 13 | 14 | public class SecondActivity extends BaseActivity implements HasComponent { 15 | 16 | @Inject 17 | Bus bus; 18 | private SecondActComponent component; 19 | 20 | @Override 21 | public void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_second); 24 | Log.i("GTAG", "sAct bus: " + bus.hashCode()); 25 | } 26 | 27 | @Override 28 | protected void initDiComponent() { 29 | component = DaggerSecondActComponent.builder() 30 | .appComponent(getAppComponent()) 31 | .secondActModule(new SecondActModule(this)) 32 | .build(); 33 | component.inject(this); 34 | } 35 | 36 | @Override 37 | public SecondActComponent getComponent() { 38 | return component; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/SecondFragment.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest; 2 | 3 | import android.os.Bundle; 4 | import android.util.Log; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | 9 | import com.opticus.daggertest.di.SecondFrComponent; 10 | import com.opticus.daggertest.managers.DbHelper; 11 | import com.opticus.daggertest.managers.ManagerB; 12 | 13 | import javax.inject.Inject; 14 | 15 | public class SecondFragment extends BaseFragment { 16 | 17 | @Inject 18 | DbHelper dbHelper; 19 | @Inject 20 | ManagerB managerB; 21 | 22 | public SecondFragment() { 23 | // Required empty public constructor 24 | } 25 | 26 | 27 | @Override 28 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 29 | Bundle savedInstanceState) { 30 | return inflater.inflate(R.layout.fragment_second, container, false); 31 | } 32 | 33 | @Override 34 | public void onActivityCreated(Bundle savedInstanceState) { 35 | super.onActivityCreated(savedInstanceState); 36 | Log.i("GTAG", "2 fr db: " + dbHelper.hashCode()); 37 | Log.i("GTAG", "2 fr bus: " + bus.hashCode()); 38 | Log.i("GTAG", "2 fr managerB: " + managerB.hashCode()); 39 | } 40 | 41 | @Override 42 | protected void initDiComponent() { 43 | SecondFrComponent.PlusComponent actComponent = getActComponent(SecondFrComponent.PlusComponent.class); 44 | SecondFrComponent component = actComponent.plusSecondFrComponent(); 45 | component.inject(this); 46 | } 47 | 48 | 49 | } 50 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/SelectActivity.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | 8 | import com.opticus.daggertest.async.AsyncInjectActivity; 9 | 10 | public class SelectActivity extends AppCompatActivity { 11 | 12 | @Override 13 | protected void onCreate(Bundle savedInstanceState) { 14 | super.onCreate(savedInstanceState); 15 | setContentView(R.layout.activity_select); 16 | } 17 | 18 | public void click1(View view) { 19 | startActivity(new Intent(this, MainActivity.class)); 20 | } 21 | 22 | public void click2(View view) { 23 | startActivity(new Intent(this, SecondActivity.class)); 24 | } 25 | 26 | public void click3(View view) { 27 | startActivity(new Intent(this, AsyncInjectActivity.class)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/async/AsyncComponent.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.async; 2 | 3 | import com.google.common.util.concurrent.ListenableFuture; 4 | import com.opticus.daggertest.managers.HugeManager; 5 | 6 | import dagger.producers.ProductionComponent; 7 | 8 | @ProductionComponent(modules = AsyncModule.class) 9 | public interface AsyncComponent { 10 | ListenableFuture hugeManager(); 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/async/AsyncInjectActivity.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.async; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | 6 | import com.google.common.util.concurrent.ListenableFuture; 7 | import com.opticus.daggertest.R; 8 | import com.opticus.daggertest.managers.HugeManager; 9 | 10 | import java.util.concurrent.ExecutionException; 11 | import java.util.concurrent.Executors; 12 | 13 | public class AsyncInjectActivity extends AppCompatActivity { 14 | 15 | @Override 16 | protected void onCreate(Bundle savedInstanceState) { 17 | initDiComponent(); 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_async_inject); 20 | } 21 | 22 | protected void initDiComponent() { 23 | AsyncComponent component = DaggerAsyncComponent 24 | .builder() 25 | .asyncModule(new AsyncModule()) 26 | .executor(Executors.newSingleThreadExecutor()) 27 | .build(); 28 | ListenableFuture future = component.hugeManager(); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/async/AsyncModule.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.async; 2 | 3 | import com.google.common.util.concurrent.Futures; 4 | import com.google.common.util.concurrent.ListenableFuture; 5 | import com.opticus.daggertest.managers.HugeManager; 6 | 7 | import dagger.producers.ProducerModule; 8 | import dagger.producers.Produces; 9 | 10 | @ProducerModule 11 | public class AsyncModule { 12 | @Produces 13 | ListenableFuture produceHugeManager() { 14 | return Futures.immediateFuture(new HugeManager()); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/ActModule.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | import com.opticus.daggertest.BaseActivity; 4 | 5 | import dagger.Module; 6 | 7 | @Module() 8 | public class ActModule { 9 | 10 | private BaseActivity activity; 11 | 12 | public ActModule(BaseActivity activity) { 13 | this.activity = activity; 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/AppComponent.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | import android.content.Context; 4 | 5 | import com.opticus.daggertest.App; 6 | import com.opticus.daggertest.di.scope.PerApplication; 7 | import com.opticus.daggertest.managers.DbHelper; 8 | 9 | import dagger.Component; 10 | 11 | @PerApplication 12 | @Component(modules = {AppModule.class}) 13 | public interface AppComponent { 14 | void inject(App app); 15 | 16 | Context context(); 17 | 18 | DbHelper dbHelper(); 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/AppModule.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | import android.content.Context; 4 | 5 | import com.opticus.daggertest.App; 6 | import com.opticus.daggertest.di.scope.PerApplication; 7 | 8 | import dagger.Module; 9 | import dagger.Provides; 10 | 11 | @Module(includes = DbModule.class) 12 | public class AppModule { 13 | protected final App application; 14 | 15 | public AppModule(App application) { 16 | this.application = application; 17 | } 18 | 19 | @PerApplication 20 | @Provides 21 | public Context provideContext() { 22 | return application.getApplicationContext(); 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/BusModule.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | import com.opticus.daggertest.di.scope.PerActivity; 4 | import com.squareup.otto.Bus; 5 | 6 | import dagger.Module; 7 | import dagger.Provides; 8 | 9 | @Module 10 | public class BusModule { 11 | @Provides 12 | @PerActivity 13 | public Bus provideBus() { 14 | return new Bus("default"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/DbModule.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | import android.content.Context; 4 | 5 | import com.opticus.daggertest.di.scope.PerApplication; 6 | import com.opticus.daggertest.managers.DbHelper; 7 | 8 | import dagger.Module; 9 | import dagger.Provides; 10 | 11 | @Module 12 | public class DbModule { 13 | @PerApplication 14 | @Provides 15 | DbHelper dbHelper(Context context) { 16 | return new DbHelper(context); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/FirstFrComponent.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | import android.content.Context; 4 | 5 | import com.opticus.daggertest.FirstFragment; 6 | import com.opticus.daggertest.di.scope.PerFragment; 7 | import com.opticus.daggertest.managers.ManagerA; 8 | import com.squareup.otto.Bus; 9 | 10 | import dagger.Component; 11 | 12 | @PerFragment 13 | @Component(dependencies = {FirstFrComponent.HasFirstFrDepends.class}) 14 | public interface FirstFrComponent { 15 | void inject(FirstFragment fragment); 16 | 17 | interface HasFirstFrDepends { 18 | Bus bus(); 19 | 20 | Context context(); 21 | 22 | ManagerA managerA(); 23 | 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/HasComponent.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | public interface HasComponent { 4 | 5 | T getComponent(); 6 | } 7 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/MainActComponent.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | import com.opticus.daggertest.MainActivity; 4 | import com.opticus.daggertest.di.scope.PerActivity; 5 | 6 | import dagger.Component; 7 | 8 | @PerActivity 9 | @Component(modules = {ActModule.class, BusModule.class}, dependencies = AppComponent.class) 10 | public interface MainActComponent extends 11 | SecondFrComponent.PlusComponent, // SubComponent way 12 | FirstFrComponent.HasFirstFrDepends {//Depends component way 13 | 14 | void inject(MainActivity activity); 15 | } 16 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/ManagerBModule.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | import com.opticus.daggertest.di.scope.PerFragment; 4 | import com.opticus.daggertest.managers.ManagerB; 5 | 6 | import dagger.Module; 7 | import dagger.Provides; 8 | 9 | @Module 10 | public class ManagerBModule { 11 | @PerFragment 12 | @Provides 13 | ManagerB provideManagerB() { 14 | return new ManagerB(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/SecondActComponent.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | import com.opticus.daggertest.SecondActivity; 4 | import com.opticus.daggertest.di.scope.PerActivity; 5 | 6 | import dagger.Component; 7 | 8 | @PerActivity 9 | @Component(dependencies = {AppComponent.class}, modules = {SecondActModule.class}) 10 | public interface SecondActComponent extends SecondFrComponent.PlusComponent { 11 | void inject(SecondActivity activity); 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/SecondActModule.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | import com.opticus.daggertest.SecondActivity; 4 | 5 | import dagger.Module; 6 | 7 | @Module(includes = BusModule.class) 8 | public class SecondActModule { 9 | private SecondActivity activity; 10 | 11 | public SecondActModule(SecondActivity activity) { 12 | this.activity = activity; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/SecondFrComponent.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di; 2 | 3 | import com.opticus.daggertest.SecondFragment; 4 | import com.opticus.daggertest.di.scope.PerFragment; 5 | 6 | import dagger.Subcomponent; 7 | 8 | 9 | @PerFragment 10 | @Subcomponent(modules = {ManagerBModule.class}) 11 | public interface SecondFrComponent { 12 | void inject(SecondFragment fragment); 13 | 14 | interface PlusComponent { 15 | SecondFrComponent plusSecondFrComponent(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/scope/PerActivity.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di.scope; 2 | 3 | import java.lang.annotation.Retention; 4 | 5 | import javax.inject.Scope; 6 | 7 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 8 | 9 | @Scope 10 | @Retention(RUNTIME) 11 | public @interface PerActivity { 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/scope/PerApplication.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di.scope; 2 | 3 | import java.lang.annotation.Retention; 4 | import java.lang.annotation.RetentionPolicy; 5 | 6 | import javax.inject.Scope; 7 | 8 | @Scope 9 | @Retention(RetentionPolicy.RUNTIME) 10 | public @interface PerApplication { 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/di/scope/PerFragment.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.di.scope; 2 | 3 | import java.lang.annotation.Retention; 4 | 5 | import javax.inject.Scope; 6 | 7 | import static java.lang.annotation.RetentionPolicy.RUNTIME; 8 | 9 | @Scope 10 | @Retention(RUNTIME) 11 | public @interface PerFragment { 12 | } 13 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/managers/DbHelper.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.managers; 2 | 3 | import android.content.Context; 4 | 5 | /** 6 | * Created by opticus on 13.10.15. 7 | */ 8 | public class DbHelper { 9 | public Context context; 10 | 11 | public DbHelper(Context context) { 12 | this.context = context; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/managers/HugeManager.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.managers; 2 | 3 | import android.util.Log; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class HugeManager { 8 | public HugeManager() { 9 | Log.i("GTAG", "init huge on: " + Thread.currentThread().getName()); 10 | try { 11 | TimeUnit.SECONDS.sleep(3); 12 | } catch (InterruptedException e) { 13 | e.printStackTrace(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/managers/ManagerA.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.managers; 2 | 3 | import android.content.Context; 4 | import android.util.Log; 5 | 6 | import com.opticus.daggertest.di.scope.PerActivity; 7 | 8 | import javax.inject.Inject; 9 | 10 | 11 | public class ManagerA { 12 | public Context context; 13 | 14 | @Inject 15 | public ManagerA(Context context) { 16 | this.context = context; 17 | } 18 | 19 | @Inject 20 | void init(Context context) { 21 | Log.i("GTAG", "method injection"); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/opticus/daggertest/managers/ManagerB.java: -------------------------------------------------------------------------------- 1 | package com.opticus.daggertest.managers; 2 | 3 | import android.content.Context; 4 | 5 | import javax.inject.Inject; 6 | 7 | public class ManagerB { 8 | 9 | @Inject 10 | void init(Context context) { 11 | 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_async_inject.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 16 | 17 |