5.2 Manage Android Runtime Changes

In this chapter  we will study about the  Android runtime environment. Android handles the runtime changes to location, language and hardware by terminating and restarting the activity again. Because of this activity resource resolution is re-evaluated and the most suitable resource value would be selected for new configurations. Applications’ response can be customized to changes.   

5.2.1 Introduction

Device configurations like screen resolution etc., changes while Android app is running. If such a change occurs activity is restarted. As we restart our computer after installing new updates to better fit with new configuration. Similar is the case with android device. It restarts the activity which was running so that all the new configurations are reloaded automatically. Activity restarts itself by calling the callback methods in the same sequence like a new activity does. We should always ensure that our application is able to restart without losing user data.

Configuration changes can be handled using two strategies and they are as follows:

  1. Retain object: An object is retained during configuration change. Activity should be allowed to restart when it encounters a configuration change. Sometimes restarting an activity might be expensive. Expensive in terms of time and resources. We might need to recover a large set of data. In this case we can use fragment and handover all the responsibilities of reconfiguration is given to the fragment so that our activity is not over burdened
  2. Handle configuration: This implies handling configuration by ourselves. We will not allow the system t restart itself. We will do it manually. A callback is received when configuration changes. This is best suitable when we have performance limitation or if the application doesn’t require an update after configuration change. Activity would mange itself. 

There is an attribute in android manifest file which listens for runtime configuration changes of an activity. Attribute is android:configChanges is added to manifest node. Some of the changes in configuration are listed as shown below:

  • Locale: Device’s language settings are changed by the user.
  • keyboardHidden: Input mechanism like keypad, D-pad, etc are either exposed or hidden.
  • mcc and mnc: Mobile country or network code has changed after a SIM has been detected.
  • fontScale: Preferred font size has been changed by user.
  • uiMode: Global user interface mode changes and this generally occurs if user switches between night mode  or day mode, car mode, etc.
  • keyboard: Either an external keyboard has been plugged in or the type of keyboard has been changed.
  • screenLayout: Layout of screen has changed. If a screen orientation changes then this is activated.
  • screenSize: It occurs when screen sizes change.
  • orientation: There are two screen orientations namely portrait and landscape. It occurs when screen has been rotated between portrait and landscape.
  • smallestScreenSize: it occurs when physical screen size of device changes.

There are situations where multiple events occur simultaneously. Resources are compiled and accessed using the R.java class file. This class file is automatically generated when app resources are complied.

5.2.2 Android Asset Packaging Tool

Adding resources to project become very easy when we use Eclipse with Android Development Tool (ADT) Plug-in. Plug-in automatically detects when we add new resources to appropriate project directory. The appropriate directory is /res. Resources are complied hence R.java file is automatically generated. This eases the use of these resources programmatically.

                AAPT tool can be found in /tools sub directory of each android SDK version. If you are not using eclipse and using any other IDE then we need to use aapt tool command line interface to compile resources and package application binaries. These app binaries would be deployed on emulator or phone.   

5.2.3 Example: R.java class

Open any one of the previous projects and find the corresponding files which are described here. For demonstration purpose I am opening LoaderEx app. Go to LoaderEx/gen/R.java. Now open the class saying R.java. You would see a class file similar to the one listed below:
Figure R.java class file

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */
package com.android.tution.Loader;
public final class R {
    public static final class attr {
    }
    public static final class dimen {
        /**  Default screen margins, per the Android Design guidelines. 
         Customize dimensions originally defined in res/values/dimens.xml (such as
         screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here.
         */
        public static final int activity_horizontal_margin=0x7f040000;
        public static final int activity_vertical_margin=0x7f040001;
    }
    public static final class drawable {
        public static final int ic_launcher=0x7f020000;
    }
    public static final class id {
        public static final int action_settings=0x7f080002;
        public static final int listView1=0x7f080001;
        public static final int textView1=0x7f080000;
    }
    public static final class layout {
        public static final int activity_main=0x7f030000;
    }
    public static final class menu {
        public static final int main=0x7f070000;
    }
    public static final class string {
        public static final int action_settings=0x7f050001;
        public static final int app_name=0x7f050000;
        public static final int hello_world=0x7f050002;
    }
    public static final class style {
        /** 
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.   
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        Base application theme for API 11+. This theme completely replaces
        AppBaseTheme from res/values/styles.xml on API 11+ devices.    
 API 11 theme customizations can go here. 
Base application theme for API 14+. This theme completely replaces
        AppBaseTheme from BOTH res/values/styles.xml and
        res/values-v11/styles.xml on API 14+ devices.
    API 14 theme customizations can go here. 
         */
        public static final int AppBaseTheme=0x7f060000;
        /**  Application theme. 
 All customizations that are NOT specific to a particular API-level can go here. 
         */
        public static final int AppTheme=0x7f060001;
    }}

Figure - R.java class file

Congratulations buddies!! We are done with this section. See you in the next section. Till then keep practicing. Happy App Developing!!!