Last active
January 29, 2019 01:16
-
-
Save jixiaoyong/db7e5f18b7106ce9cb36b61bf7134340 to your computer and use it in GitHub Desktop.
Dagger 2 在Android中的使用——Activity篇
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* author: jixiaoyong | |
* email: jixiaoyong1995@gmail.com | |
* website: https://jixiaoyong.github.io | |
* date: 2019/1/27 | |
* description: todo | |
*/ | |
class MainApplication : HasActivityInjector, Application() { | |
override fun onCreate() { | |
super.onCreate() | |
DaggerAppComponent.create().inject(this) | |
} | |
@Inject | |
lateinit var dispatchActivityInjector: DispatchingAndroidInjector<Activity> | |
override fun activityInjector() = dispatchActivityInjector | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* author: jixiaoyong | |
* email: jixiaoyong1995@gmail.com | |
* website: https://jixiaoyong.github.io | |
* date: 2019/1/27 | |
* description: todo | |
*/ | |
class MainActivity : AppCompatActivity() { | |
@Inject | |
lateinit var waitForInjectClass: WaitForInjectClass | |
override fun onCreate(savedInstanceState: Bundle?) { | |
AndroidInjection.inject(this) | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_dagger2_x_android_main) | |
Log.d("TAG", "The Class is $waitForInjectClass") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* author: jixiaoyong | |
* email: jixiaoyong1995@gmail.com | |
* website: https://jixiaoyong.github.io | |
* date: 2019/1/27 | |
* description: todo | |
*/ | |
@Component(modules = [AndroidInjectionModule::class, MainActivityModule::class]) | |
interface AppComponent { | |
fun inject(application: MainApplication) | |
} | |
@Subcomponent | |
interface MainActivitySubComponent : AndroidInjector<MainActivity> { | |
@Subcomponent.Builder | |
abstract class Builder : AndroidInjector.Builder<MainActivity>() | |
} | |
@Module(includes = [MainActivityModule.InnerModule::class], subcomponents = [MainActivitySubComponent::class]) | |
class MainActivityModule { | |
@Provides | |
fun bindWaitForInjectClass() = WaitForInjectClass() | |
@Module | |
abstract class InnerModule { | |
@Binds | |
@IntoMap | |
@ClassKey(MainActivity::class) | |
abstract fun bindInjectorFactory(builder: MainActivitySubComponent.Builder): AndroidInjector.Factory<*> | |
} | |
} | |
class WaitForInjectClass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment