Tuesday, 22 January 2013

Android Development Tutorial (Based on Android 4.1) class-4

Android Application Architecture


1. AndroidManifest.xml


The components and settings of an Android application are described in the AndroidManifest.xml file. For example all Activities and Services of the application must be declared in this file.

It must also contain the required permissions for the application. For example if the application requires network access it must be specified here.

code

The package attribute defines the base package for the Java objects referred to in this 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 uses its own unique package. Therefore it is a good habit to use your reverse domain name as package name. This will avoid collisions with other Android applications.

android:versionName and android:versionCode specify the version of your application. versionName is what the user sees and can be any String.

versionCode must be an integer. The Android Market determine based on the versionCode, if it should perform an update of the applications for the existing installations. You typically start with "1" and increase this value by one, if you roll-out a new version of your application.

The <activity> tag defines an Activity, in this example pointing to the Convert class in the de.vogella.android.temperature package. An intent filter is registered for this class which defines that this Activity is started once the application starts (action android:name="android.intent.action.MAIN" ). The category definition category android:name="android.intent.category.LAUNCHER" defines that this application is added to the application directory on the Android device.

The @string/app_name value refers to resource files which contain the actual value of the application name. The usage of resource file makes it easy to provide different resources, e.g. strings, colors, icons, for different devices and makes it easy to translate applications.

The uses-sdk part of the AndroidManifest.xml file defines the minimal SDK version for which your application is valid. This will prevent your application being installed on unsupported devices.

2. Activities and Lifecycle


The Android system controls the lifecycle of your application. At any time the Android system may stop or destroy your application, e.g. because of an incoming call. The Android system defines a lifecycle for Activities via predefined methods. The most important methods are:


  • onSaveInstanceState() - called after the Activity is stopped. 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 re-started, can be used to initialize fields



 3. Configuration Change


An Activity will also be restarted, if a so called "configuration change" happens. A configuration change happens if an event is triggered which may be relevant for the application. For example if the user changes the orientation of the device (vertically or horizontally). Android assumes that an Activity might want to use different resources for these orientations and restarts the Activity.

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

You can avoid a restart of your application for certain configuration changes via the configChanges attribute on your Activity definition in your AndroidManifest.xml. The following Activity will not be restarted in case of orientation changes or position of the physical keyboard (hidden / visible).

code2

4. Context


The class android.content.Context provides the connection to the Android system and the resources of the project. It is the interface to global information about the application environment.

The Context also provides access to Android Services, e.g. the Location Service.

Activities and Services extend the Context class.

Related Topics : SCJP Sun Certified Programmer for Java 6 Study Guide Exam(310-065) Class-3

No comments:

Post a Comment