Android activity in detail with example


         In simple words, activity is a screen with which the user interacts. For example, email application will display an activity to compose emails, another activity to view list of emails in inbox and so on. So any application that we develop will have at least one activity. The more complicated your application, the more screens you are likely to need.
Among these several screens/activities one screen can be your primary interface screen / main activity which handles the main UI functionality. Though an application can have several activities, only one can be in active state at a time.

Creating Activities
           Its quite simple to create an activity. Simply extend Activity class in your main activity to create it.

public class MainActivity extends Activity {}

Within this class we must define the UI and implement our functionality. Below is the Activity skeleton code layout.

package com.example.androidactivity;
import android.os.Bundle;
import android.app.Activity;

public class MainActivity extends Activity {
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
 }
}
Above code will display an empty screen upon execution. Lets place an UI control to the activity that displays some information to the user to make it more interactive. To assign UI to an activity call setContentView from the onCreate method of your Activity.
Create a text view control under res/layout/activity_main.xml

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

Add this layout to your Main activity with the below code:

setContentView(R.layout.activity_main);

Register Activity in Manifest
One more step before we execute and see the output. To use an activity in our application we need to register it in Androidmanifest.xml. Add the below code snippet to your manifest.

<activity
            android:name="com.example.androidactivity.MainActivity"
            android:label="@string/app_name" >
 </activity>

Note: Eclipse takes cares of  adding these snippets when you create a new activity by default. 

Change default activity in Android
As we discussed earlier, an application can have more than one activity, but only one activity can be your main activity that is displayed when we run the application. By default MainActivity is set as our default activity. This is done by adding the intent filter as shown below:

<intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>

If you want to change your default activity to something else, then place this code between that activity.

That's it now execute your application to see the output.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

MainActivity.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {

  @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

Download source code
AndroidActivity.rar 

Hope this provides a clear explanation on what an activity is, how to create activity and how to change your default activity in android application. Share your views !




Reactions:

1 comment :