Using Intent Demo

Resources » Forums » Android - Examples > Using Intent Demo
January 26, 2010 7:45:04 AM PST (3 years ago). Seen 70,154 times. 10 replies.
Photo Serete Itebete
Member since Dec 23, 2009
Location: Oakland
Forum Posts: 17
After writing a single activity, there comes a need to transition to another activity to perform another task either with or without information from the first activity.

Android platform allows transition by means of Intent Interface.

In this example there are two activities - IntentActionDemo.java and IntentA.java that both extend the super class Activity. Do not forget to declare any new activity in the AndroidManifest.xml with permission.

I have introduced buttons in both activities which have a listener to allow an intent to transition from one intent to the other activity and vis-versa.



Simple intent example;
Note that optional step 2 was not used in our demo.
Step 1: Intent i = new Intent(context, NameOfClassToTransitionTo.class)

Step 2:(Optional)Intents can take various forms that make it even carry data in key/name pairs ie i.putExtra("key1", "My first Info")
i.putExtra("key2", "My second Info")

Step 3: startActivity(i)

IntentActionDemo.java
Code:

package com.marakana.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class IntentActionDemo extends Activity implements OnClickListener {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button = (Button) findViewById(R.id.intentButton);
button.setOnClickListener(this);

}

@Override
public void onClick(View src) {
Intent i = new Intent(this, IntentA.class);
startActivity(i);
}
}


IntentA.java

Code:

package com.marakana.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class IntentA extends Activity implements OnClickListener{

@Override
public void onClick(View src) {
Intent i = new Intent(this, IntentActionDemo.class);
startActivity(i);
}

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intenta);

Button button = (Button) findViewById(R.id.ButtonIntentA);
button.setOnClickListener(this);
}
}


AndroidManifest.xml
Code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.marakana.com"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".IntentActionDemo"
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="IntentA"></activity>
</application>
<uses-sdk android:minSdkVersion="3" />

</manifest>



string.xml

Code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello</string>
<string name="app_name">IntentActionDemo</string>
<string name="IntentA">Click Next intent</string>
<string name="world">World!</string>
<string name="Intent0">Click to Previous Intent</string>
</resources>


main.xml
Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:textSize="18sp"/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/intentButton" android:text="@string/IntentA"></Button>
</LinearLayout>


intenta.xml
Code:

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

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/TextViewWorld" android:text="@string/world" android:textSize="18sp"></TextView>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/ButtonIntentA" android:text="@string/Intent0"></Button>
</LinearLayout>



Screenshot



Source
http://marakana.com/static/tutorials/IntentActionDemo.zip
Edited 5 times. Last edit by Bala Krishnan on Oct 26, 2010 at 6:31:37 AM (about 2 years ago).
January 28, 2010 2:17:16 AM PST (3 years ago)
Photo Uzair Ali
ABC
Member since Jan 28, 2010
Forum Posts: 1
Intent i = new Intent(context, NameOfClassToTransitionTo.class);

I get an error when i use the above line:
The constructor Intent(new View.OnClickListener(){}, Class<IntentA>) is undefined ...

How to resolve this issue??

Thanks
April 26, 2010 11:08:33 PM PDT (3 years ago)
Photo Serete Itebete
Member since Dec 23, 2009
Location: Oakland
Forum Posts: 17
Uzair,

Make sure that the class (NameOfClassToTransitionTo.class) exists in your project. Try going to project>clean>build

See if that works. Let me know if that works
August 11, 2010 9:37:23 PM PDT (2 years ago)
Photo Rajesh Kumar
Svcet
Member since Aug 11, 2010
Forum Posts: 1
hi ....i am new to android development. plz send me android examples
Edited 2 times. Last edit by Rajesh Kumar on Aug 11, 2010 at 9:39:22 PM (about 2 years ago).
August 27, 2010 5:56:56 AM PDT (2 years ago)
Photo Sarun Khumthong
Mahidol University
Member since Aug 27, 2010
Forum Posts: 1
You have to add activity to the Manifest too.
October 26, 2010 6:31:37 AM PDT (2 years ago)
Photo Bala Krishnan
Android
Company
Member since Oct 24, 2010
Forum Posts: 2
I m facing error while runnning this code ask to force close
July 6, 2011 4:36:37 AM PDT (one year ago)
Photo Vootla Radha
Gameshastra solutions
Member since Jul 6, 2011
Forum Posts: 1
iam new to android sdk...i have one dought, wht is the main use of intent?
October 14, 2011 11:20:07 PM PDT (one year ago)
Photo Firzan Ghulam
Itechnologix
Member since Oct 14, 2011
Forum Posts: 1
plz send me the source code for android chat application...!!!
March 25, 2012 2:35:06 AM PDT (one year ago)
Photo Stefano Bizzi
BluePine Technology
Member since Mar 25, 2012
Forum Posts: 1
Serete,
This will look a bit like a useless post, but every time I need a quick way to do something I stumble upon an example written by you that does EXACTLY what I need, nothing more, nothing less :) thanks!
March 30, 2012 4:40:56 AM PDT (one year ago)
Photo Korada Rameshbabu
Xpio
Member since Mar 30, 2012
Forum Posts: 3
hi sir,
Please explain the use of intent.putextras(String,String);
function.
what the use
August 27, 2012 2:38:29 AM PDT (42 weeks ago)
Photo Narendra Kumar Vaddi
SPTCS
Member since Aug 27, 2012
Forum Posts: 1
When ever i try to pass data using intent,exceptions occurred. What i supposed to do to avoid that problem?
please send the reply sir.