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

tryGetInstanceFromComponentCache

fun <T : Any> tryGetInstanceFromComponentCache(component: Any, clazz: KClass<T>): 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.

You should use this before tryGetInstanceFromComponentReflection as the reflection method may create new instances instead of reusing them due to scoping limitations. This can be avoided to some degree if your @Scope annotations are @Retention(RUNTIME).

Rather then using this function directly you can use tryGetInstanceFromComponent which tries this first then the reflection method.

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