Preventing Activity Restart on Orientation Change in Android Applications

Nov 23, 2025 · Programming · 8 views · 7.8

Keywords: Android Configuration Changes | Activity Restart | Screen Orientation Lock

Abstract: This article provides an in-depth analysis of preventing Activity restarts during screen orientation or keyboard state changes in Android applications through the android:configChanges attribute. It examines the mechanism of configuring parameters like keyboardHidden, orientation, and screenSize in AndroidManifest.xml, offering compatibility solutions for different API levels. The importance of proper application state preservation is emphasized to ensure stability across various configuration change scenarios.

Problem Background and Core Challenges

In Android application development, many developers prefer to lock their applications in specific orientations (such as portrait mode), typically achieved by setting android:screenOrientation="portrait" in AndroidManifest.xml. While this approach effectively prevents screen orientation changes on most devices, certain specific devices (like early HTC G1 models) experience unexpected Activity restarts and state loss when physical keyboards are deployed, despite maintaining screen orientation.

Custom Handling Mechanism for Configuration Changes

Android provides the android:configChanges attribute, allowing developers to declare configuration change types that their applications will handle independently. When specified configuration changes occur, the system doesn't restart the Activity but instead calls the onConfigurationChanged() method.

Basic configuration example:

<activity android:name="MainActivity"
     android:screenOrientation="portrait"
     android:configChanges="keyboardHidden|orientation">
</activity>

This configuration informs the system that the application will handle keyboard visibility (keyboardHidden) and screen orientation (orientation) changes independently, eliminating the need for Activity restart.

Compatibility Extensions for Android 3.2 and Above

Starting from Android 3.2 (API level 13), screen orientation switches also change the "screen size" configuration. Therefore, applications targeting API level 13 or higher must additionally include the screenSize configuration:

<activity
    android:name="MainActivity"
    android:screenOrientation="portrait"
    android:configChanges="keyboardHidden|orientation|screenSize">
</activity>

It's important to note that if the application targets API level 12 or lower, the system automatically handles screenSize changes without explicit declaration.

Important Considerations for Configuration Handling

While configuration changes can prevent Activity restarts due to orientation or keyboard changes, developers must consider:

1. Applications may be interrupted for other reasons (such as incoming calls or system resource constraints), necessitating proper state preservation implementation

2. Over-reliance on configuration change handling may prevent proper response to system resource changes

3. A balance between user experience and system compatibility should be struck when selecting the most appropriate handling approach

Supplementary Handling Approaches

In certain scenarios, developers may need to execute specific operations during configuration changes. This can be achieved by overriding the onConfigurationChanged method:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Add custom handling logic here
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

This approach allows maintaining screen orientation while performing necessary interface adjustments during configuration changes.

Best Practice Recommendations

Considering various factors, developers are advised to:

• Configure configChanges attributes appropriately based on target API levels

• Always implement comprehensive state preservation and restoration mechanisms

• Properly handle interface adaptation in the onConfigurationChanged method

• Thoroughly test compatibility across different devices and Android versions

Through reasonable configuration and complete error handling, applications can ensure stable user experiences across various configuration change scenarios.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.