MΓ³dulo 15 Β· Dependency Injection with Hilt β LecciΓ³n 3 de 4 Β· ~8 min
Scopes & HiltViewModel
One instance for the app, or a fresh one each time
By default, every @Inject or @Provides binding gives out a brand-new instance every time something asks. That's fine for something cheap and stateless β but WalkerRepository holds the Flow the whole app reads walk data from (module 14); if every ViewModel got its own repository instance, they'd each be watching a different in-memory object and could disagree.
@Singleton fixes that: one instance for the lifetime of the whole app, shared by everyone who asks. Put @Singleton next to @Inject constructor on WalkerRepository itself, or on a @Provides function, and Hilt builds it once and hands out that same instance forever after.