Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service Context in Hilt #2442

Open
tompee26 opened this issue Mar 1, 2021 · 2 comments
Open

Service Context in Hilt #2442

tompee26 opened this issue Mar 1, 2021 · 2 comments

Comments

@tompee26
Copy link

tompee26 commented Mar 1, 2021

Why is there no ServiceContext qualifier? How can one obtain the service context in a module installed in ServiceComponent?

@danysantiago
Copy link
Member

There is indeed no @ServiceContext in Hilt, we need to add one. However, the Service itself is an available binding in the ServiceComponent, so you can write a module that provides it:

@Module
@InstallIn(ServiceComponent::class)
abstract class ServiceContextModule {
  @Binds
  @MyServiceContextQualifier // <- Create a qualifier for it
  abstract fun provideContext(service: Service): Context
}

or you can just inject Service where as you would use the context since they are the same (Service is a Context).

@Chang-Eric
Copy link
Member

What @danysantiago said is correct, but I would say I'm not sure we really should add an @ServiceContext. It isn't clear to me that the qualifier adds value over injecting Service directly. The same could be said for @ActivityContext which I am not sure yet if I regret adding. Most code I see injects Activity directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants