Skip to content

Instantly share code, notes, and snippets.

@kirich1409
Last active November 12, 2019 06:46
Show Gist options
  • Save kirich1409/b57c7227c0670a4d2c9ba02f1439f350 to your computer and use it in GitHub Desktop.
Save kirich1409/b57c7227c0670a4d2c9ba02f1439f350 to your computer and use it in GitHub Desktop.
@RequiresApi(Build.VERSION_CODES.P)
// We still need default constructor for AppComponentFactory
class InjectComponentFactory : AppComponentFactory() {
private lateinit var application: SampleApplication
private val activityProviders: Map<String, Provider<Activity>> by lazy {
application.appComponent.activityProviders
.mapKeys { (key, _) -> key.name }
}
override fun instantiateApplication(
cl: ClassLoader, className: String
): Application {
val instantiateApplication = super.instantiateApplication(cl, className)
// Save reference to the Application instance
this.application = instantiateApplication as SampleApplication
return instantiateApplication
}
override fun instantiateActivity(
cl: ClassLoader, className: String, intent: Intent?
): Activity {
return activityProviders[className]?.get()
?: super.instantiateActivity(cl, className, intent)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment