ViewModelが回転後も保持される理由
構成変更後も状態を保持します。
「ViewModelが回転後も保持される理由」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The Rotation Problem
Rotate the screen and Android destroys your Activity, wiping any state you stored in it. Your counter, text, and scroll position all vanish. 😵
Configuration Changes
Rotation is one of many configuration changes: theme switches, locale changes, and window resizing all recreate your screen the same way.
Enter the ViewModel
A ViewModel is a class built to hold UI state that outlives the Activity it serves, so a rotation no longer throws your data away.
class CounterViewModel : ViewModel() {
var count = 0
}Tied to the Screen, Not the Activity
The ViewModel is scoped to the screen’s lifecycle owner, so the same instance is handed back to the recreated Activity instead of a fresh one.
How It Survives
Android keeps the ViewModel in a retained store across the configuration change, then reconnects it to your brand-new Activity automatically.
When It Is Cleared
A ViewModel lives until the screen is truly finished. When the user navigates away for good, onCleared() runs so you can release resources.
override fun onCleared() {
// cancel jobs, close streams
}Not for Everything
The ViewModel survives rotation but not process death. For that, pair it with SavedStateHandle so critical values can be restored later.
Add the Dependency
You get ViewModel support from the lifecycle libraries. Add the lifecycle-viewmodel-compose artifact so Compose can obtain one easily.
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.8.0")Separation of Concerns
Beyond survival, a ViewModel keeps logic out of your UI. Your Composables stay focused on drawing while the ViewModel owns the state.
A Mental Model
Think of the ViewModel as a quiet backstage manager: the Activity comes and goes, but the manager keeps your show’s state ready to perform again.
Why This Matters
Surviving rotation means users never lose progress mid-task. That reliability is the foundation every robust Compose app is built on. ✨
Quick Check
Test your grasp of why a ViewModel outlives a rotation.
Recap
Rotation recreates your Activity, but a ViewModel survives configuration changes and reattaches, keeping UI state intact and logic out of the UI.
よくある質問
「ViewModelが回転後も保持される理由」レッスンは無料ですか?
はい。「ViewModelが回転後も保持される理由」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「ViewModelが回転後も保持される理由」で何を学びますか?
構成変更後も状態を保持します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。
「ViewModelが回転後も保持される理由」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- ViewModelが回転後も保持される理由
- Composable内のviewModel()
- UiStateデータクラスをモデル化する
- collectAsStateWithLifecycle