Thursday, 12 January 2017

Handling Screen Configuration changes

Handling the screen orientation changes on your android device is sometimes the most irritating and frustrating part in your development cycle. So in this post i will try to remove that frustration and will tell you what exactly happens during the screen orientation process.

When we rotate our device and the screen changes orientation, android system usually destroys our application’s existing activities or fragments and restarts the running activity/fragment(onDestroy() is called, followed by onCreate()). Android System do this so that our application can reload all the resources based on the new configuration.

To work around this thing, Android gives us the option to save our app’s state before destroying all of our activities and fragments, and also to restore that state when recreating them. Thus, proper handling of orientation changes centers around saving this state and also avoiding memory leaks.
So to properly handle a restart, it is important that our activity restores its previous state through the normal activity lifecycle, in which Android calls onSaveInstanceState() before it destroys our activity so that we can save our data about the application state. We can then restore the state during onCreate() or onRestoreInstanceState().

While it may seem a bit hectic to implement this all, handling screen orientation changes properly provides you with several benefits as well:
- You will be able to easily use alternate layouts in portrait and landscape orientations, and you will be able to handle many exceptional states such as low memory situations and interruptions from incoming phone calls without any extra code.

Implementation:
The first thing that you should do is set the android:configChanges flag on your Activity in AndroidManifest.xml as shown below:

<activity
    android:name=".YourActivity"
    android:label="@string/activity_one"
    android:configChanges="orientation|screenSize|keyboardHidden" />

This flag signals to the Android platform that you are going to manually handle your screen orientation, screenSize and keyboard appearance/disappearance changes for this Activity.

Now, when one of these above mentioned configurations change, YourActivity does not restart. Instead, the YourActivity receives a call to onConfigurationChanged() method. This method has a Configuration object that specifies the new device configuration. By reading fields in the Configuration, you can determine the new configuration and make appropriate changes by updating the resources used in your interface. At the time this method is called, your activity's Resources object is updated to return resources based on the new configuration, so you can easily reset elements of your UI without the system restarting your activity.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // Do your stuff in landscape mode
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
       // Do your stuff in portrait mode 
       Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

The Configuration object represents all of the current configurations, not just the ones that have changed.
If you don't need to update your application based on these configuration changes, you can instead not implement onConfigurationChanged(). In this case, all of the resources used before the configuration change are still used and you've only avoided the restart of your activity.

Note: When you declare your activity to handle a configuration change, you are responsible for resetting any elements for which you provide alternatives. If you declare your activity to handle the orientation change and have images that should change between landscape and portrait, you must re-assign each resource to each element during onConfigurationChanged().

That's all.
Share and comment if any issues.
References: https://developer.android.com/guide/topics/resources/runtime-changes.html

No comments:

Post a Comment