BookNest now has three layers: the data layer (BookRepository, later Room 234 in Local Storage with Room and Retrofit 43,944 in Retrofit and Ktor), the state holders (CatalogViewModel, scoped to the catalog entry, and CartViewModel, scoped to the activity so that every screen shares one cart), and the UI (stateless screens plus thin route composables). BookNestNav swapped its remembered list for the cart ViewModel, and the detail and cart screens now send CartEvents:
val cartVm: CartViewModel = viewModel() // scoped to the activity
val cart by cartVm.cart.collectAsStateWithLifecycle()The logs show what each piece survives. A first launch, opening Salt and Saffron and adding it:
D/BookNestVM: MainActivity onCreate, restored=false
D/BookNestVM: CartViewModel created, cart=[]
D/BookNestNav: now at com.example.booknest.ui.nav.Catalog
D/BookNestVM: CatalogViewModel created
D/BookNestVM: books: first collector arrived
D/BookNestVM: loaded: Success
D/BookNestNav: now at com.example.booknest.ui.nav.BookDetail/{id} id=3
D/BookNestVM: books: upstream stoppedRotating to landscape and back recreated the activity twice (MainActivity onCreate, restored=true, twice) and created no ViewModel and loaded nothing: both instances survived. Then Home, adb shell am kill com.example.booknest to simulate the system reclaiming the process (Process Death), and a relaunch:
D/BookNestVM: MainActivity onCreate, restored=true D/BookNestVM: CartViewModel created, cart=[3] D/BookNestNav: now at com.example.booknest.ui.nav.Catalog D/BookNestVM: CatalogViewModel created D/BookNestVM: books: first collector arrived D/BookNestVM: loaded: Success
Both ViewModels were new, but the cart came back as [3] from SavedStateHandle and the catalog reloaded. The search query was lost, since it lives only in a MutableStateFlow. Save what the user would hate to lose, reload what can be fetched again, and keep real carts in storage (Room, Local Storage with Room).