Sunteți pe pagina 1din 13

Intent – a jargon in Android is doing all the work at background.

Go through the post, you will


learn how to navigate from one screen to another.

What is Intent?

o Intents are system messages, running around the inside of the device, notifying
applications of various events, from hardware state changes (e.g.,an SD card was
inserted), to incoming data (e.g., an SMS message arrived),to application events
(e.g., your activity was launched from the device’s main menu).

 Not only can you respond to intents, but you can create your own, to launch other
activities, or to let you know when specific situations arise (e.g., raise such-and-so intent
when the user click this button).
 Intents are asynchronous messages which allow Android components to request
functionality from other components of the Android system. For example an Activity can
send an Intents to the Android system which starts another Activity.
 Three of the core components of an application — activities, services, and broadcast
receivers — are activated through messages, called intents [Read more at android
developers site].

What are we going to develop?

A simple application which has two screens:

Screen1 contains three controls – TextView shows ‘Enter your name’ text, Edittext to get name
from the user and Button to take user to next screen

Screen2 contains only one control – TextView which displays greet message

Project Structure

Layout creation:

 Create new android project [File >> New >> Android Project] with project name IntentExample
 Click next and select target android device version [I chose version 2.2]
 Click next and enter package name – com.prgguru.android
 Click finish

Code Listings

Create layout for Screen 1(Main.xml):


1

2 <?xml version="1.0" encoding="utf-8"?>

3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

4 android:layout_width="fill_parent"

android:layout_height="fill_parent"
5
android:orientation="vertical"
6
<TextView
7
android:id="@+id/textView1"
8
android:layout_width="match_parent"
9
android:layout_height="wrap_content"
10 android:text="Enter your name"

11 android:textAppearance="?android:attr/textAppearanceLarge" />

12 <EditText

13 android:id="@+id/editText1"

14 android:layout_width="match_parent"

android:layout_height="wrap_content"
15
android:ems="10" >
16
<requestFocus />
17
</EditText>
18
<Button
19
android:id="@+id/button1"
20 android:layout_width="match_parent"

21 android:layout_height="wrap_content"

22 android:text="Take me to next screen" />

23</LinearLayout>

24

Layout for Screen 1 should look like:


Create layout for Screen 2:
Create new layout xml named greeting.xml under /res/layout

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

5 android:orientation="vertical" >

6 <TextView

7 android:id="@+id/textView1"

8 android:layout_width="match_parent"

android:layout_height="wrap_content"
9
android:textAppearance="?android:attr/textAppearanceLarge"
10
android:text="TEXT"/>
11
</LinearLayout>
12

Layout for Screen 2 should look like:


We have to create two java activity classes to handle two screens:

1. IntentExampleActivity.java
2. GreetingActivity.java

1. IntentExampleActivity.java

Create following objects under IntentExampleActivity class:

1EditText nameEditCtrl;

2Button btnCtlr;

3String name;

Refer the controls and set the listener for button inside onCreate method of
IntentExampleActivity class:

1nameEditCtrl = (EditText) findViewById(R.id.editText1);

2btnCtlr = (Button) findViewById(R.id.button1);

3btnCtlr.setOnClickListener(new ButtonClickHandler());

Create Button listener class for button1 inside IntentExampleActivity class:

1 public class ButtonClickHandler implements View.OnClickListener {


2 //When button is clicked

3 public void onClick(View view) {


//If name field is not empty, name variable is assigned with entered name
4
if (nameEditCtrl != null && nameEditCtrl.getText().length() != 0) {
5
name = nameEditCtrl.getText().toString();
6
}
7
//If name is not entered, String 'Guest' is assigned to name variable
8
else{
9 name ="Guest";

10}

11//Create Intent object which moves from IntentExampleActivity class

12//to GreetingActivity class


Intent intObj = new
13
Intent(IntentExampleActivity.this,GreetingActivity.class);
14//Set user entered name in value name which will be

15//used in GreetingActivity class

16intObj.putExtra("USERNAME", name);

17//Start GreetingActivity

18startActivity(intObj);
}
19
}
20

21

2. GreetingActivity.java

Create Java class named ‘GreetingActivity’ under com.prgguru.android package and replace the
default skeleton with below one:

1 package com.prgguru.android;
2 import android.app.Activity;
3 import android.content.Intent;
4 import android.os.Bundle;
5 import android.widget.TextView;
6 public class GreetingActivity extends Activity {
TextView greetMsg;
7 /** Called when the activity is first created. */
8 @Override
9 public void onCreate(Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
setContentView(R.layout.greeting);
11 greetMsg = (TextView) findViewById(R.id.textView1);
12 //Assign the intent that started this activity
13 Intent intename = getIntent();
14 //Get the USERNAME passed from IntentExampleActivity
15 String uname = (String) intename.getSerializableExtra("USERNAME");
//Set text for greetMsg TextView
16 greetMsg.setText("Welcome " + uname);
17 }
18
19}
20
21
22

Add GreetingActivity in AndroidManifest.xml under application tag:

<activity android:name=".GreetingActivity"
android:label="@string/app_name" >
</activity>
r Android device
Posts Tagged How to move from one activity
to another using an intent
Now, lets see how the components in android communicate.

Communication between the three core components in android-activity, service and broadcast
receiver is made possible using intents. Intents can be said as messages passed between these
components. An Intent object is a bundle of information containing information such as the
action to be performed, data on which the action is to be performed etc.

Intents are of two types: Explicit intents and Implicit intents

Here , in this post, I’m discussing about Explicit Intent.

Explicit intents: They specify the target component by its name.That is, they explicitly specify
the target component which needs to respond to it.
Here is how it is done:

Intent intent=new Intent(source.this,destination.class);

Or

Intent intent=new Intent();


Intent.setClass(source.this,destination.class);

Lets see what an explicit intent is and how it works through a simple example.

Code for the Source activity :

public class Source extends Activity {


/** Called when the activity is first created. */
Button dest;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dest=(Button)findViewById(R.id.click);
dest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(Source.this,Destination.class);
startActivity(intent);
}
});
}
}

The XML code for main.xml:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to go to destination"
android:textSize="20dip" />
</LinearLayout>

This is how the UI looks:


Code for the Destination activity:

public class Destination extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.maindest);
}
}

XML code for maindest.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"
android:background="#FFF">
<TextView
android:id="@+id/textView1"
android:layout_width="278dp"
android:layout_height="wrap_content"
android:text="Welcome to Destination"
android:textSize="25dip"
android:textColor="#0000ff" />
</LinearLayout>

The Output When the Button on the Source UI is clicked :

Hope this helped you in understanding an Intent and how it works.

S-ar putea să vă placă și