Sunday, January 17, 2010

Explaining Hello Android application

Now we’re going to take a deeper look on the Hello Android application and know what each line of code exactly means
The HelloAndroid.java file contains the following code:
package mina.android.helloandroid;

import android.app.Activity;
import android.os.Bundle;

public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}


  1. The first line is the package name of the application.
  2. Lines 3,4 are import statements of two libraries():
    android.app.Activity
    android.os.Bundle
  3. Line 6 is the declaration of the HelloAndroid class which extends from
    Activity
    , Activity represents the User interface of your application much like windows or web forms.
  4. Our class contains one function which is onCreate(Bundle savedInstanceState). This function is called when the activity is first created, it should contain all the initialization and UI setup.
    This function has a parameter savedInstanceState of type Bundle
    this object holds the activity’s previously saved state if exists (similar to asp.net view state concept)
  5. Line 10 calls the super class’ OnCreateMethod with the activity’s
    savedInstanceState

  6. Line 11 calls
    setContentView(R.layout.main);
    method which dsiplays the layout definition in main.xml file in res/layout directory by accessing a reference to it from the R class
    This method is required to display the user interface, if the activity does not include this function then it would display a blank screen

Summary
The basic android application with only one view consists of a class that extends the Activity class. This class preserves the activity frozen state (like viewstate in asp.net) and it displays the UI defined in xml file or defined explicitly programmatically.

No comments:

Post a Comment