BeCuriouzBeCuriouz

Learn something new every day

📱 Mobile Programming
🔄

Rotate your Android phone and the screen restarts — on purpose

💡 Rotating an Android device destroys and recreates the Activity, so any UI state you want to keep must be saved into a Bundle, held in a ViewModel, or persisted to storage.

Turn an Android phone sideways and something drastic happens under the hood: the system destroys your entire Activity and builds a fresh one. This is a configuration change — rotation, dark-mode toggling, language switches, resizing in multi-window, or plugging into a monitor all count. Because the device configuration changed, Android assumes your UI might need completely different resources (a landscape layout, larger drawables, translated strings), so it runs onDestroy and then a brand-new onCreate.

The catch: any state you stored as a plain variable inside the Activity is wiped. That's why a half-typed form field or a scroll position can silently reset on rotation if you didn't plan for it. The Activity object you were using is gone — a different instance is now on screen.

Android gives you tiered ways to survive this. For small UI state, rememberSaveable in Compose (or onSaveInstanceState in the older View system) stashes values into a Bundle that the new instance restores. For heavier screen state, a ViewModel deliberately lives across the recreation and isn't destroyed. And for data that must outlast the app entirely, you use local storage like a database or DataStore.

You can opt out with android:configChanges to handle rotation yourself, but the docs treat that as a last resort — recreation exists so your app automatically picks up the right resources for the new configuration.

🔄 Rotate your Android phone and the screen restarts — on purpose — BeCuriouz