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 tutorial we will learn about

What is Activity??

  • A window that contains interface to which user interacts, called Activity.
  • Generally one android application has multiple activities to which user interacts, like send SMS, take a photo etc.
  • We can have Activities of different sizes like activity that fills the screen, activity that shows up as notification etc.
  • As we maintained earlier that there might be multiple activities in an application, developer decides which activity will be launched first using intent -filter we will see that in our exmaple.

  • Activity can start another activity.
  • Android maintains a stack of activities, say example when user navigates from one activity to another, the previous activity stops and goes to the stack called “back stack”. so when user presses the back button, the activity which entered last in the stack, resumes.
  • Every activity has callback methods like create, resume, paused etc, you can override these methods. These methods are discussed in detail in Activity life cycle.
  • Each activity has a life cycle which has many phases, we will discuss activity life cycle in a while.
  • There is one more feature called fragments, you can think of fragments in a way that many fragments can be grouped together to make an activity. we will discuss about fragments in detail later.
  • Another important concept called Intent , basically Activities communicate with each other using this concept. we will discuss in detail about intent later.


How to create an Activity

  • Create an java class and extends the Activity base class.
  • Activity must override onCreate() method, this method is called when activity starts.
  • Inside the onCreate() method you must call setContentView() method, this method decides the layout of you activity which will be defined in some XML file. Example setContentView(R.layout.activity_main), here activity_main.xml will define the layout of this activity.
SimpleAcivity

SimpleAcivity

NOTE: Every Activity must be declared in AndroidMainfest.xml file to make it run. You can think of AndroidManifest.xml file as a monitor, who manages every activities in the application.

AndroidManifest

AndroidManifest


Start an Activity

As I have mentioned earlier in this post that one activity can start another activity. It does that using startActivity().

Steps:

  • Create a new Intent by passing the activity name which you want to start.( To know about Intent click here)
  • call the startActivity() by passing Intent object as parameter.
  • Optional : Activity can send some data to another activity while starting using intent.putExtra, we will see an example in a while.

Complete Example :

In this example we will create two activities and one activity 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 Activity :

First Activity

First Activity

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>

Second Activity :

Second Activity

Second 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 Androiding 🙂
Sumit Jain

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

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 *