Explain the Android Activity lifecycle and what happens when a user rotates the screen.
Model answer
Activity lifecycle: onCreate → onStart → onResume (active) → onPause → onStop → onDestroy. When the screen rotates: the current Activity is destroyed and recreated (onDestroy → onCreate). This causes data loss unless preserved. Solutions: (1) ViewModel (Jetpack): survives configuration changes; the ViewModel is NOT destroyed on rotation, so UI state stored in ViewModel is preserved; (2) onSaveInstanceState(): bundle-based state preservation for simple data (strings, primitives) — called before onStop; (3) rememberSaveable in Jetpack Compose: equivalent to onSaveInstanceState for Compose state. Best practice: store ephemeral UI state in ViewModel (user input, list scroll position), persistent data in Room or DataStore, and avoid putting large objects in savedInstanceState bundles. In Jetpack Compose, Activity is often used as a host; composables manage their own lifecycle through LaunchedEffect, DisposableEffect, and LifecycleOwner.