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); } }
- The first line is the package name of the application.
- Lines 3,4 are import statements of two libraries():
android.app.Activity
android.os.Bundle
- 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.
- 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)
- Line 10 calls the super class’ OnCreateMethod with the activity’s
savedInstanceState
- 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
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