gh-pages / com.nextfaze.devfun.inject.dagger2 / tryGetInstanceFromComponent

tryGetInstanceFromComponent

fun <T : Any> tryGetInstanceFromComponent(component: Any, clazz: KClass<T>, cacheResolvedTypes: Boolean = true): T? (source)

Helper function to be used on Dagger 2.x Component implementations.

Will traverse the component providers and modules for an instance type matching clazz - scoping is not considered.

This function delegates to tryGetInstanceFromComponentCache and tryGetInstanceFromComponentReflection. If you are having issues with new instances being created when they shouldn’t be, ensure your @Scope annotations are @Retention(RUNTIME).

Alternatively use @Dagger2Component on your functions/properties (or @get:Dagger2Component for property getters) that return components to tell DevFun where to find them (they can be whatever/where ever; static, in your app class, activity class, etc) - which will end up using this method anyway.

See Also

Dagger2Component