回転をまたぐrememberSaveable
構成変更後も状態を保持します。
「回転をまたぐrememberSaveable」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The Rotation Problem
Rotate the screen and your tap counter snaps back to zero. That is because a configuration change recreates the whole Activity.
remember Isn't Enough
remember survives recomposition, but not Activity recreation. When the Activity restarts, remembered state is gone for good.
What Triggers Recreation
Rotation, language switches, and dark-mode toggles are configuration changes. Each one tears down and rebuilds your UI from scratch.
Meet rememberSaveable
rememberSaveable works like remember but also saves the value into the saved instance state, so it survives recreation.
var count by rememberSaveable { mutableStateOf(0) }Just Swap the Call
Often the fix is a one-word change: replace remember with rememberSaveable and your counter now keeps its value after rotation.
What Can Be Saved
It saves types that fit in a Bundle out of the box: Int, String, Boolean, and other primitives. Most simple state just works.
var name by rememberSaveable { mutableStateOf("") }Custom Types Need a Saver
For your own classes, give rememberSaveable a Saver that tells it how to store and restore the object as bundle-safe data.
Data Classes Made Easy
For Kotlin data classes you can use the mapSaver or listSaver helpers, or annotate with Parcelize, to make saving simple.
@Parcelize
data class User(val name: String) : ParcelableIt Is Not Forever
rememberSaveable survives rotation and brief backgrounding, but not a full app kill. For permanent data you still need storage.
When to Reach for It
Use rememberSaveable for transient UI state you want to keep through rotation, like a counter, a typed query, or a tab index.
remember vs Saveable
Rule of thumb: use remember for cheap or recomputable values, and rememberSaveable when losing the value on rotation would annoy the user.
Quick Check
Which API keeps state through a screen rotation?
Recap
You learned that rememberSaveable keeps transient UI state alive through rotation and config changes, while remember does not. Nicely done! 🎉
よくある質問
「回転をまたぐrememberSaveable」レッスンは無料ですか?
はい。「回転をまたぐrememberSaveable」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。
「回転をまたぐrememberSaveable」で何を学びますか?
構成変更後も状態を保持します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Jetpack Compose Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「回転をまたぐrememberSaveable」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このJetpack Compose Academyレッスンでコードを書いて実行できますか?
はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- rememberとmutableStateOf
- 再コンポジション:再描画のきっかけ
- タップカウンターを作る
- 回転をまたぐrememberSaveable