rememberSaveable durante a Rotação
Mantenha o estado durante mudanças de configuração.
rememberSaveable durante a Rotação é uma aula grátis de Jetpack Compose Academy no CoddyKit. Esta é a aula 4 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Jetpack Compose Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Jetpack Compose Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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! 🎉
Perguntas Frequentes
A aula “rememberSaveable durante a Rotação” é grátis?
Sim — o texto completo de “rememberSaveable durante a Rotação” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Jetpack Compose Academy, atualize para CoddyKit PRO. O curso de Jetpack Compose Academy inclui 4 aulas no total.
O que vou aprender em “rememberSaveable durante a Rotação”?
Mantenha o estado durante mudanças de configuração. Você pratica Jetpack Compose Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Jetpack Compose Academy?
Nenhuma experiência prévia é necessária. Jetpack Compose Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 4 de 4.
Quanto tempo leva a aula “rememberSaveable durante a Rotação”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Jetpack Compose Academy?
Sim. Cada aula de Jetpack Compose Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- remember e mutableStateOf
- Recomposição: O que Aciona um Redesenho
- Criando um Contador de Toques
- rememberSaveable durante a Rotação