Tag Archives: android application architecture

ANDROID APPLICATION ARCHITECTURE

16 Sep

The Components and settings of an Android application are described in the AndroidManifest.xml file. It should also contain the permissions for the application.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="de.vogella.android.temperature"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Convert"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="9" />

</manifest>

The Package attribute defines the base package for the java objects referred to in file. If a Java object lies within a different package. It must be declared with the full qualified package name Google Play requires that every Android Application has it own unique package. Hence, it is good to use reverser domain name as package name. It will avoid confusions and Collisions with other applications. android:versionName and android:versionCode specify the version of your application version name is what the user sees and can be an integer. The Android Market determination is based on the version code. You start with “1” if you need it do perform an update of existing installations and increase the “1” value by one if you roll-out a new version of your application.

The <activity> tag defines an Activity in this example pointing to convert class in the com.example package. Intent filter is registered for this.

R.Java and Resource

The gen directory is an Android project contains the generated values R.jave is a generated class that contains references to certain resources of the project resources must be defined in the “res” directory and can be XML files icons or pictures.

If you are creating new resources the reference is automatically created in R.Jave via the Eclipse ADT tools. These are static integer values and define IDs for the resources. In order to access the corresponding resource via these IDs the Android System provides methods.

R.Java is automatically created by the Eclipse development environment manual changes are not necessary and will be overridden by the tooling.

Assets

The “assets” directory can be used to store any kind of data while the “res” directory contains structured values which are known to the Android platform. You can access this data via the “Assets Manager” through which you can access the “get Assets” method.

// Get the AssetManager
AssetManager manager = getAssets();

// Read a Bitmap from Assets
try {
InputStream open = manager.open("logo.png");
Bitmap bitmap = BitmapFactory.decodeStream(open);
// Assign the bitmap to an ImageView in this layout
ImageView view = (ImageView) findViewById(R.id.imageView1);
view.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}

 Activities and Layouts

Via “Layouts” the user interface for “Activities” is defined. The layout defines the included “Views” (Widgets) and it properties.

A Layout can be defined either through java code or through XML. Most of the since it is defined in XML file only. The Layouts are defined via resource file in the /res/layout folder. This specifies he  “View Groups”, Views, for this layout.

To access a View through Java Code, you have to give it a unique ID via the “android:id” attribute. To assign a new ID to a “view” use @+id/yourvalue .

<Button
	android:id="@+id/button1"
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:text="Show Preferences" >
</Button>

Layouts are defined is XML because this separates the Logic from the layout definition. If also allows the definitions of different layouts. You can mix both approaches.

 Reference to resource is XML files

In XML files you can refer to other resource via the @ sign.

Activities and Life cycle

The Android system Controls the life cycle of your Application. The Android system may stop or destroy your application. Through predefined methods the Android system defines a life cycle “Activities” Important are:

  • onSaveInstanceState() – Called offer the Activity is stopped it is used to save data so that the “Activity ” Can restore its states if re-started
  • onPause()- always called if the Activity ends. Can be used to release resource or save data
  • onResume() – called if the “Activity” is restarted can be used to initialize fields.

Configuration Change

When Configuration on change happens  an Activity will be restarted. It An event which is relevant for the application is triggered configuration change occurs* for eg: if the user Changes the orientation of the device (either vertically or horizontally) Android assumes that an ‘Activity’ may wanted to use different resources for those orientations and restarts the Activity.

In the emulator you can stimulate the change of the orientation via Ctrl+F11

Restart of application can be avoided by following in case of orientation change or position of the physical keyboard.

<activity android:name=".ProgressTestActivity"
     android:label="@string/app_name"
     android:configChanges="orientation|keyboardHidden|keyboard">
</activity>

Context

The Class “android content context” provides connection between Android system and the resources of the projects. It is the interface about the application environment to the global information. The context also provides access to Android Services Activities and services extend the context class.