Orientation Change in Honeycomb
Introduction
Honeycomb, the Android 3.0 release, introduced significant changes to the way applications handle orientation changes. Prior to Honeycomb, applications would typically recreate their entire activity when the device’s orientation changed (e.g., from portrait to landscape). This could lead to performance issues and a jarring user experience.
Honeycomb’s Approach
Honeycomb addressed this by introducing a new mechanism for handling orientation changes. Instead of recreating the entire activity, the system now reconfigures the existing activity. This results in a much smoother and more efficient user experience.
Key Changes in Honeycomb
- Activity Lifecycle Changes: The activity lifecycle was modified to include a new state called
onConfigurationChanged()
. This method is called when the device’s configuration changes, such as orientation, locale, or screen size. - Configuration Changes: The following are the most common configuration changes:
- Orientation: Portrait to Landscape or Landscape to Portrait
- Locale: Language/Region
- Screen Size: Physical screen size, density, etc.
- Default Configuration Handling: By default, Honeycomb will automatically handle configuration changes by calling
onConfigurationChanged()
, thus keeping the existing activity instance running.
Handling Orientation Changes
To handle orientation changes in your application, you should override the onConfigurationChanged()
method in your activity.
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Check for orientation changes if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { // Landscape mode } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { // Portrait mode } }
Preventing Activity Recreation
To prevent the activity from being recreated when the orientation changes, you need to declare the android:configChanges
attribute in your AndroidManifest.xml
file. This attribute tells the system which configuration changes the activity can handle without being recreated.
<activity android:name=".MyActivity" android:configChanges="orientation|screenSize" />
Advantages of Honeycomb’s Approach
- Improved Performance: Activities are not recreated, leading to less memory consumption and faster response times.
- Smoother User Experience: There are no noticeable pauses or disruptions when the orientation changes.
- Simplified Development: Developers can focus on handling configuration changes within a single activity instance.
Comparison Table
Feature | Pre-Honeycomb | Honeycomb and Above |
---|---|---|
Activity Lifecycle | Activity is recreated on orientation change. | Activity is reconfigured (onConfigurationChanged() is called). |
User Experience | Jarring, with noticeable pauses. | Smooth and seamless. |
Performance | Can be slow, especially on lower-end devices. | Much faster and more efficient. |
Development Complexity | More complex, requiring handling of activity creation and state preservation. | Simpler, as developers only need to handle configuration changes within the existing activity. |
Conclusion
Honeycomb’s new approach to handling orientation changes is a significant improvement over the previous methods. It delivers a more efficient, smoother, and less resource-intensive user experience, making development easier for Android developers.