Android Dagger2 Runtime Inject Parameter or Object

Create a module to provide runtime value using singleton object as storage.

// a module to provide value via singleton object@Moduleinternal class RuntimeModule {    @Provides    fun provideUsername() = RuntimeInjection.username}
// singleton object as storageobject RuntimeInjection {    var username: String = ""}
// Assign value to singleton object during runtimeRuntimeInjection.username = "Desmond"

NOTE: Make sure to include Module via @Module(includes = [RuntimeModule::class]) in AppModule or @Component(modules = [..., [RuntimeModule::class]) in AppComponent.

NOTE: If you are providing multiple values of the same type object (e.g. username and password, which are both string), use Qualifiers.

NOTE: I am not sure if this is a good practice/pattern.

NOTE: If the parameter/object is available during application startup, you can use Binding Instances.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.