I'm adapting my project to utilize DI - Dagger in particular. I have no problems injecting classes, but how do you 'inject' a runtime generated variable?
Say, I want to store user's login session object that contains data such as api-token
, loggedUsername
and whatnot inside. This object is generated at runtime when the user has successfully logged in, in another class responsible for doing so.
As an illustration, what I want is to simply do something like this:
public class FooPresenter {
...
Session mSession;
@Inject FooPresenter(Session session, ...) {
mSession = session;
...
}
...
}
Thanks in advance!
I ended up declaring a method to obtain my session, which I kept as a
String
inSharedPreferences
after a successful login, in a Dagger module class.That way, I could just inject the session anywhere it's needed.
Here's my
SessionDomainModule
:By doing that, I could achieve what I wanted and codes in the like of those on my question could finally work.