Skip to content

Instantly share code, notes, and snippets.

@ValeryPonomarenko
Created September 14, 2018 11:46
Show Gist options
  • Save ValeryPonomarenko/cc71bd8191b390da3d4d61869e78930c to your computer and use it in GitHub Desktop.
Save ValeryPonomarenko/cc71bd8191b390da3d4d61869e78930c to your computer and use it in GitHub Desktop.
Lifecycle aware dagger components
class SomeApplication : Application() {
private lateinit var appComponent: AppComponent
private var featureComponent: FeatureComponent? = null
override fun onCreate() {
super.onCreate()
appComponent = DaggerAppComponent.builder()
.someModule(SomeModule())
.build()
}
fun getFeatureComponent(): FeatureComponent {
val component = featureComponent
if (component == null) {
component = DaggerFeatureComponent.bulder()
.appDependencies(appComponent)
.anotherModule(AnotherModule())
.build()
}
return component.also { featureComponent = it }
}
fun removeComponent() {
featureComponent = null
}
//so on...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment