State and Configuration Changes

Currently, Coda Pizza has a small problem, and it will instantly be familiar to you. Run Coda Pizza and add a topping or two to the pizza. Then rotate your device or emulator.

Yep. The topping selections are lost after the configuration change. remember persists state across recompositions, but it has its limits: When your Activity is destroyed and re-created, so is your composition. Because the composition is discarded, it will restart from a blank slate when your Activity is re-created and calls setContent.

This was not an issue when you declared the pizza state as a global variable. But now, because your state is associated with your composition hierarchy – and, by extension, your activity – it ...

Get Android Programming: The Big Nerd Ranch Guide, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.