Last active
November 12, 2019 06:46
-
-
Save kirich1409/b57c7227c0670a4d2c9ba02f1439f350 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@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