Intent In Android

Android intent is basically a class that is used to activate a new activity in android application.

Android intents are of two types

1) Implicit intent

2) Explicit intent

Explicit intent are basically used to invoke the component.

Android intent class is used to invoke components in an android application.

In this tutorial , I will explain how to invoke a new activity  and pass values to the new activity using android intents.

Let us create two activity .

Activity 1:

Main.java (in the scr folder)

main.xml ( in the res/layout folder)

First I will describe the code for Main.java :

In the main.xml file, add one textview and button.

Android automatically assigns default id to every control that is added on the user interface file.

The id assigned to the controls, is used in the Main.java file to reference the controls (textview, button).

package com.test.intent;

import android.app.activity

public class Main extends Activity

{

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

//The below step assigns the layout main.xml , that is stored in the res/layout folder

setContentView(R.layout.main);

// Below code gets the reference of the controls from the main.xml file

// R.layout.button1 where button1 is the id of the button

Button btn=(Button)findViewById(R.layout.button1);

final EditText et=(EditText)findViewById(R.layout.editText1);

b.setOnClickListener(new onClickListener())

{
@override

public void onClick()
{
//Here we will define our intent class
// Intent class takes two parameters

// the class which is calling the activity
// The class which is called.

Intent intnt=new Intent(Main.this,Second.class);

//now we will pass the value of edit text to the second class

// intnt.putExtra() function is used to send message
// putExtra() function takes two parameter
// 1) the name 2) the value

// value contains the text that is taken from the edittext
// “thetext” is the identifier that second.java will reference to get the value

intnt.putExtra(“thetext”,et.getText().toString());

//Now we will start the activity using startActivity method.

startActivity(intnt);

}

});

}

}

2) Now we will create the second file

Activity 2

a) second.java in the src folder

b) second.xml in the res/layout folder

In the second.xml file , I have put a Textview to show the value that we will receive from the intent.

package com.test.Intent;

import android.app.activity;

public class Second extends Activity
{

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

//The below step assigns the layout second.xml , that is stored in the res/layout folder

setContentView(R.layout.second);

// Again we will find the reference of the textview and then assign the text that we will get from the
// calling intent.

// we use putExtra to send values from one activity to another

// In order to getvalues using intent , we use getExtra()

TextView tv=(TextView)findViewById(R.layout.textview1);

tv.setText(getIntent().getExtra().getStrig(“thetext”));

}

}

Note: You can use your own package name and other id for your controls. So android intents are used to invoke

components. Android intent can pass values between two activities.