Pr.Pg Next Pg

Starting an Activity with Intent


  • An intent is an abstract description of an operation to be performed.

  • An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

  • An Intent is exactly what it describes. It's an "intention" to do an action.

  • An Intent is basically a message to say you did or want something to happen. Depending on the intent, apps or the OS might be listening for it and will react accordingly.


Starting the Activity

  • The activity Example which we have defined earlier, inorder to use it we need to start it via some call.

  • This is facilitated by Intent.

  • In our tutorial, we will call the Example activity when the button Change Activity is clicked.

  • In order to do so we have to make certain modifications to the code of as shown.


package com.example.helloandroid;


import android.os.Bundle;


import android.content.Intent;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;


public class MainActivity extends Activity {

int counter = 0;

Button click;

TextView display;



protected void onCreate(Bundle savedInstanceState) {



click = (Button) findViewById(;

display = (TextView) findViewById(;


click.setOnClickListener(new View.OnClickListener() {



public void onClick(View arg0) {

// TODO Auto-generated method stub

display.setText("I have been clicked " + (++counter) + " times");





change_activity.setOnClickListener(new View.OnClickListener() {



public void onClick(View arg0) {

// TODO Auto-generated method stub

Intent in = new Intent("com.example.helloandroid.EXAMPLE");








  • As we can see in the code, we have delared a new button named change_activity.

  • We then as expected assign it the reference of the button from the XML file with the help of its ID, the same way we did it for the other Views.

  • We then also define the function to be performed when the button is clicked the same way we did for the button Click Me.

  • But in this case, we have to start a new activity, i.e, Example.

  • In order to do so, we have to create an Intent object.

  • While creating the object we have to pass the string which contains the action name of the activityto be started.

  • Thus we have a string “com.example.helloandroid.EXAMPLE” .

  • We can check this name in the AndroidManifest.xml file.

  • Finally we have to start the activity by using the startActivity() method in which we pass the Intent object which we have just created.

  • Finally when we run the App we can see the changes.

  • The following screenshots will help you understand how the code actually works.

  • Note that the Click Me button will function in the same manner as before.



Pr.Pg border                                              Next Pg