Skip to content
Commitwise
Menu

Scope (dependency injection)

How long an injected object lives and who shares it: one per app, per screen, or a new one every time.

Definition

In Hilt, a scope annotation ties a binding to a component's lifetime: @Singleton (the whole app), @ActivityRetainedScoped (an activity across rotation), @ViewModelScoped (one ViewModel), @ActivityScoped. Unscoped bindings create a new instance for every injection. Scope expensive or stateful objects that must be shared, such as HTTP clients and databases, and leave stateless helpers unscoped.

In practice

An unscoped OkHttpClient provider gives every repository its own client and connection pool.

Related terms

Esc

Type to search the whole site.