0Pricing
Jetpack Compose Academy · レッスン

hiltViewModelと@HiltViewModel

ComposableにViewModelを注入します。

「hiltViewModelと@HiltViewModel」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

ViewModels Need Dependencies Too

Your ViewModel usually depends on a repository or use case. Hilt can inject those for you, so you never build them by hand again.

Annotate the ViewModel

Mark the class with @HiltViewModel and add @Inject to its constructor. Hilt now knows how to create it with all its dependencies.

@HiltViewModel
class FeedViewModel @Inject constructor(private val repo: Repo) : ViewModel()

Inject Anything It Needs

Constructor parameters can be any type Hilt knows, like a repository, a use case, or a logger. Hilt resolves the whole chain for you.

@HiltViewModel
class FeedViewModel @Inject constructor(
    repo: Repo, logger: Logger
) : ViewModel()

Get One in Compose

Inside a Composable, call hiltViewModel() to obtain a Hilt-built ViewModel. No factory boilerplate, just one clean call.

@Composable
fun FeedScreen(vm: FeedViewModel = hiltViewModel()) { }

Add the Compose Dependency

hiltViewModel() lives in the hilt-navigation-compose artifact. Add it to your Gradle file so the function is available in your screens.

implementation("androidx.hilt:hilt-navigation-compose:1.2.0")

Default Parameter Pattern

Putting hiltViewModel() as a default parameter keeps the Composable testable. A test can still pass a fake ViewModel directly.

@Composable
fun FeedScreen(vm: FeedViewModel = hiltViewModel())

Scoped to the Owner

hiltViewModel() scopes the instance to the nearest ViewModelStoreOwner, usually the Activity or nav destination, so it survives recomposition.

One ViewModel Per Destination

Inside a NavHost, each destination gets its own ViewModel instance. Navigate away and back, and that screen receives a fresh one.

Mark the Activity Too

The host Activity must carry @AndroidEntryPoint so Hilt can deliver ViewModels into its Composables. Without it, injection fails.

@AndroidEntryPoint
class MainActivity : ComponentActivity()

Expose State, Hide Logic

The injected ViewModel exposes UI state and the screen just reads it. Your Composable stays thin while logic lives in one tested place.

val state by vm.uiState.collectAsStateWithLifecycle()

No Manual Factories

Before Hilt you wrote a ViewModelProvider.Factory for each ViewModel with arguments. Hilt removes that boilerplate completely. 🎁

Quick Check

Which pair correctly sets up a Hilt-injected ViewModel for a Composable?

Recap: Injected ViewModels

You learned to tag a ViewModel with @HiltViewModel and fetch it via hiltViewModel(), scoped to its owner with zero factory code. ✨

よくある質問

「hiltViewModelと@HiltViewModel」レッスンは無料ですか?

はい。「hiltViewModelと@HiltViewModel」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。

「hiltViewModelと@HiltViewModel」で何を学びますか?

ComposableにViewModelを注入します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Jetpack Compose Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「hiltViewModelと@HiltViewModel」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このJetpack Compose Academyレッスンでコードを書いて実行できますか?

はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. DIの理由:テスト可能で疎結合なコード
  2. モジュール、@Provides、@Binds
  3. hiltViewModelと@HiltViewModel
  4. スコープとコンポーネントのライフタイム
← Jetpack Compose Academyに戻る