Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

Android Activities — Definition, Create and Start an Activity

In this tuto­r­ial we will learn about

What is Activity??

  • A win­dow that con­tains inter­face to which user inter­acts, called Activity.
  • Gen­er­ally one android appli­ca­tion has mul­ti­ple activ­i­ties to which user inter­acts, like send SMS, take a photo etc.
  • We can have Activ­i­ties of dif­fer­ent sizes like activ­ity that fills the screen, activ­ity that shows up as noti­fi­ca­tion etc.
  • As we main­tained ear­lier that there might be mul­ti­ple activ­i­ties in an appli­ca­tion, devel­oper decides which activ­ity will be launched first using intent –fil­ter we will see that in our exmaple.

  • Activ­ity can start another activity.
  • Android main­tains a stack of activ­i­ties, say exam­ple when user nav­i­gates from one activ­ity to another, the pre­vi­ous activ­ity stops and goes to the stack called “back stack”. so when user presses the back but­ton, the activ­ity which entered last in the stack, resumes.
  • Every activ­ity has call­back meth­ods like cre­ate, resume, paused etc, you can over­ride these meth­ods. These meth­ods are dis­cussed in detail in Activ­ity life cycle.
  • Each activ­ity has a life cycle which has many phases, we will dis­cuss activ­ity life cycle in a while.
  • There is one more fea­ture called frag­ments, you can think of frag­ments in a way that many frag­ments can be grouped together to make an activ­ity. we will dis­cuss about frag­ments in detail later.
  • Another impor­tant con­cept called Intent , basi­cally Activ­i­ties com­mu­ni­cate with each other using this con­cept. we will dis­cuss in detail about intent later.


How to cre­ate an Activity

  • Cre­ate an java class and extends the Activ­ity base class.
  • Activ­ity must over­ride onCre­ate() method, this method is called when activ­ity starts.
  • Inside the onCre­ate() method you must call set­Con­tentView() method, this method decides the lay­out of you activ­ity which will be defined in some XML file. Exam­ple setContentView(R.layout.activity_main), here activity_main.xml will define the lay­out of this activity.
SimpleAcivity

Sim­pleAciv­ity

NOTE: Every Activ­ity must be declared in AndroidMainfest.xml file to make it run. You can think of AndroidManifest.xml file as a mon­i­tor, who man­ages every activ­i­ties in the application.

AndroidManifest

Android­Man­i­fest


Start an Activity

As I have men­tioned ear­lier in this post that one activ­ity can start another activ­ity. It does that using star­tAc­tiv­ity().

Steps:

  • Cre­ate a new Intent by pass­ing the activ­ity name which you want to start.( To know about Intent click here)
  • call the star­tAc­tiv­ity() by pass­ing Intent object as parameter.
  • Optional : Activ­ity can send some data to another activ­ity while start­ing using intent.putExtra, we will see an exam­ple in a while.

Com­plete Example :

In this exam­ple we will cre­ate two activ­i­ties and one activ­ity will invoke the another activity.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.simpleactivity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name = "com.example.simpleactivity.SecondActivity" />"
    </application>

</manifest>

First Activ­ity :

First Activity

First Activ­ity

Below is the java code and xml file.

MainActivity.java

package com.example.simpleactivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void startSecond(View v){
    	Toast.makeText(this, "Starting Second Acivity" , Toast.LENGTH_SHORT).show();
    	Intent i = new Intent(this, SecondActivity.class);
    	startActivity(i);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="First Activity"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20sp"
        android:layout_gravity="center"
        android:text="Start Second Activity"
        android:onClick="startSecond" />

</LinearLayout>

Sec­ond Activity :

Second Activity

Sec­ond Activity

Java code and xml file.

SecondActivity.Java

package com.example.simpleactivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class SecondActivity extends Activity {
	 @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.second);
	        Toast.makeText(this, "I am  Second Acivity" , Toast.LENGTH_SHORT).show();
	    }
}

second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Second Activity"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

Now Let’s Test our program

Start Another Activity

Start Another Activity

Happy Android­ing 🙂
Sumit Jain

You may also like...

2 Responses

  1. November 16, 2014

    […] our ear­lier post “Cre­ate and Start Activ­ity” you have […]

  2. November 18, 2014

    […] our ear­lier post “start Activ­ity” and ” Activ­ity Send Data To Another Activ­ity” you have […]

Leave a Reply

Your email address will not be published. Required fields are marked *