Created
September 14, 2018 11:46
-
-
Save ValeryPonomarenko/cc71bd8191b390da3d4d61869e78930c to your computer and use it in GitHub Desktop.
Lifecycle aware dagger components
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
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