Saturday, May 25, 2013

Lesson 7. Android Activity

What is Android Activity ?


What is Android Activity ?

An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View). While activities are often presented to the user as full-screen windows
  • The basis of android applications
  • A single Activity defines a single viewable screen
  • Can have multiple per application
  • Each is a separate entity
  • They have a structured life cycle
  • Different events in their life happen either via the user touching buttons or programmatically

The user interface of an application is displayed on a device through an Activity, typically with one Activity created for each unique screen. Internally there is a stack of Activities, when moving from one screen to another, the next Activity to be visible is pushed onto the top of the stack – put another way, the Activity on the top of the stack is what is visible on the display. Activities are popped from the stack by pressing the back button, which resumes the previous Activity.

Activity Class

When you want to create a new Activity, you extend the Activity class. The code below shows a simple starting point:

package com.learnsimply.basicactivity;

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

Notice the reference to main in the layou
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
}


Once created you’ll need to associate a layout of the UI for the Activity. The recommended approach is to use an XML-based layout. For example, the layout below shows how you might define a simple table.

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1">
    <TableRow>
        <TextView
            android:text="@string/row1a"/>
        <TextView
            android:text="@string/row1b"
            android:gravity="right"/>
    </TableRow>
    <TableRow>
        <TextView
            android:text="@string/row2a"/>
        <TextView
            android:text="@string/row2b"
            android:gravity="right"/>
    </TableRow>
</TableLayout>



Let’s assume the layout about was created/saved in a file main.xml. Behind the scenes, the Eclipse plugin will update the R.java file and make a reference to the layout resources defined in main.xml. As an example:


package com.learnsimply.basicactivity;
 
public final class R {
    public static final class attr {
    }
    public static final class drawable {
        public static final int icon=0x7f020000;
    }
    public static final class layout {
        public static final int main=0x7f030000;
    }
    public static final class string {
        public static final int app_name=0x7f040004;
        public static final int row1a=0x7f040000;
        public static final int row1b=0x7f040001;
        public static final int row2a=0x7f040002;
        public static final int row2b=0x7f040003;
    }
}

Notice the reference to main in the layout class. 
We can now update the BasicTable class defined above, 
which extended the Activity class, to tie the layout (table) to the Activity we defined.  we do this be calling the setContentView() method inside onCreate():
package com.learnsimply.basicactivity;
 
import android.app.Activity;
import android.os.Bundle;
 
public class BasicActivity extends Activity 
{
  WebView webView;
 
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
 
    // Associate table layout to this Activity
    setContentView(R.layout.main);
}
}
Android Manifest
For each Activity, there needs to be an entry in the AndroidManifest.xml file,where you can define a theme,label,permissions, etc.
Here is a sample manifest with one Activity:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.tabletest.table"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TableActivity"
                  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>
</manifest>

No comments:

Post a Comment