Modern Approach: States in Jetpack Compose
In modern development with Jetpack Compose, we no longer usually "block" rotation in the Manifest. Instead, the correct approach is to make our interface capable of surviving activity recreation by preserving its state.
State preservation with rememberSaveable
Unlike the standard remember (which is lost upon rotation), in Compose we use rememberSaveable so that data survives configuration changes:
// In Compose, this survives screen rotation automatically var name by rememberSaveable { mutableStateOf("") } TextField( value = name, onValueChange = { name = it }, label = { Text("Name") } )Using ViewModel
Current best practice is to delegate the state to a ViewModel. ViewModels are designed to remain in memory while the Activity is recreated due to a rotation.
class MyViewModel : ViewModel() { // This state persists even if the screen rotates var uiState by mutableStateOf(MyState()) }With this modern approach, we allow Android to manage screen resources natively (loading different designs if necessary) without losing the information the user has already entered.
Often when testing our Android application, whether on an emulator or a physical device, we can see that when we rotate the screen, activities restart by default. This results in text being cleared from fields or critical interface states being lost.
This happens because when the device is rotated, the onCreate method is invoked again following the basic Android lifecycle.
Legacy Approach: Configuration in the AndroidManifest
In traditional development based on XML and Activities, a quick solution to avoid restarting is to add the android:configChanges attribute in the activity tag within the AndroidManifest.xml:
android:configChanges="screenSize|orientation|screenLayout"A more complete example:
<activity android:name=".MainActivity" android:configChanges="screenSize|orientation|screenLayout"> </activity>What does each attribute mean?
- orientation: Detects when the user has rotated the physical screen.
- screenSize: Triggers when the available resolution changes (when rotating between landscape and portrait).
- screenLayout: Ensures that other layout changes do not restart the activity.
Important note: If you disable restarting in an activity that has different layout files for horizontal and vertical, the system will not load the new layout automatically upon rotation. You must handle the change manually in the onConfigurationChanged method.
Why not configure all activities to not restart by default?
It depends on how our activities are configured; as you may know, when designing an activity for multiple resolutions and screen positions (landscape/portrait), we might need to define multiple layouts per activity:

This means that if we disable restarting for an activity that presents multiple layouts and rotate the mobile device, the layout will not update.