Intent is a very good feature provided in Android.
The name itself suggests that Intent means some intention to do a
specific work. Intent is used for making call to new
activity from current activity and sending/Receiving data. Suppose we
have an Activity "MainMessageTask" in our app and we want to call "ChildMessageTask" activity from "MainMessageTask". At that time we need to use Intent for making call to child activity. We can also send and receive data from different calling activity with using intent. This is a very first use of Intent in android. " So the Intents are the system messages that will transfer data from Parent Activity to Child Activity and notify about various events done by user. For Example if we remove and insert a memory card on our phone it will notified by intent in android system by developer of the application, and this to be done by using "Implicit Intent". so the main difference between Implicit and Explicit Intent is that...
So the very first thing we will doing is that user generated message is transferred to child activity after clicking on Send Message to Child Activty button. as shown in Figure 01.
[This is our "MainMessageTask.class" activity].
ChildMessageTask.class is called by startActivityForResult(messageIntent); , data is already bundled in messageIntent object by messageIntent.putExtras("Messages"); generated message is displaying on child activity screen. here user also responds back to the main activity. as shown in Figure 02.
[This is our "ChildMessageTask.class" activity], Messages from MainMessageTask.class is fetched by messageIntent.getExtras("Message"); screen message is given back to Main activity as shown in Figure 03.
[This is our MainMessageTask.class activity]. Result by ChildMessageTask is fetch in MainMessageTask by overriding public void onActivityResult(int requestCode,int resultCode,Intent data);
"Code for Main Activity MainMessageTask.class"
"Code for Main Activity ChildMessageTask.class"
<activity android:name=".ChildMessageTask" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
So the final conclusion of this article is that Intent is very useful in our apps some of the advantages of intents is given here...
- Explicit Intent :- Explicit Intent is used to call another activity by Java class file loading from Parent Activity.
- Implicit Intent :- Implicit Intent is used to call services without calling java class file within the system messages. For example if we want to call web site in our application then we need to pass that site`s URL from implicit intent with using URI.
- Syntax of Intent : Intent <Intent-name> = new Intent ( <application context of current activity>, <Calling Activity Class name.class> );
- This is available in class "android.content.Intent" since API Level 1.
- after making object of intent we need to add data to this object so the syntax for that is... <Intent-name> . putExtras(<Message>); here putExtras is given in class intent ... "android.content.Intent.putExtras(Bundle extras)" and method given in class intent is as follows.. "Public Intent putExtras(String,Bundle);" and "Public Intent putExtras(String);"
- next we need to launch another activity by using two methods
- public void startActivity(Intent <Intent-name>); parameters: <Intent> for intent to start.
- public void startActivityForResult(Intent <Intent-name>, int requestCode); parameters: <Intent> for intent to start, <requestCode> >=0, and used after retiring from the child activity specify by developer.
- here one question came up in mind is that Which one is going to be best choice out of above two methods in our application? So, answer is very simple if we need to launch an activity as a "Parent-Child" concept then we requires to call method "startActivityForResult(Intent <Intent-name>, int requestCode);" and if we need to launch only activity without getting result back from child activity then we requires to call method "startActivity(Intent <Intent-name>);" .
- In our example we will gona use "startActivityForResult(Intent <Intent-name>, int requestCode); ". Because we launch activity using "Parent-Child" concept.
- Let first understand output of our example..
![]() |
[Figure 01: Main screen for sending messages to child activity] |
![]() |
[Figure 02: Child screen which responds back to main activity] |
![]() |
[Figure 03: Main screen which gets messages from Child screen] |
[This is our "MainMessageTask.class" activity].
ChildMessageTask.class is called by startActivityForResult(messageIntent); , data is already bundled in messageIntent object by messageIntent.putExtras("Messages"); generated message is displaying on child activity screen. here user also responds back to the main activity. as shown in Figure 02.
[This is our "ChildMessageTask.class" activity], Messages from MainMessageTask.class is fetched by messageIntent.getExtras("Message"); screen message is given back to Main activity as shown in Figure 03.
[This is our MainMessageTask.class activity]. Result by ChildMessageTask is fetch in MainMessageTask by overriding public void onActivityResult(int requestCode,int resultCode,Intent data);
- So by just understanding above architecture we will going to see complete code of the example...
"Code for Main Activity MainMessageTask.class"
package com.helloworkShop.intentdemo;
/**
* This is a Main activity that demonstrates the Explicit Intent concept.
*
*/
public class MainMessageTask extends Activity implements OnClickListener {
private Button mySendMessage;
private EditText myMessage;
private TextView displayMessageFromChildScreen;
//Methods
/**
* onActivityResult - called when the child activity finishes its execution after calling finish method from child activity...
* This is where you should get data from previous activity [from child activity]
*
* Always called by child activity after calling finish method from ChildMessageTask...
* @param requestCode int
* @param resultCode int
* @param data Intent
*/
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
displayMessageFromChildScreen =
(TextView) findViewById (R.id. displayMessageFromChildActivity);
String Message= data.getStringExtra("BackResultToMainActivity");
displayMessageFromChildScreen.setText("Message from Child Activity :- " + Message);
}
}
/**
* onCreate - called when the activity is first created.
* This is where you should do all of your normal static set up: create views, bind data to lists, etc.
*
* Always followed by onStart().
*
*/
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mmt);
myMessage= (EditText) findViewById(R.id.Messages);
mySendMessage= (Button) findViewById(R.id.SendMessage);
mySendMessage.setOnClickListener(this);
}//end of onCreate method...
/**
* onClick - call when the "Send Message to Child Activity" button clicked.
* This is where you should do all of intent calling procedure.
*
*/
@Override
public void onClick(View button) {
String strMyMessage=myMessage.getText().toString();
myMessage.setText("");
if (button = = mySendMessage){
// setting up myBundle object for storing messages.
Bundle myBundle=new Bundle();
myBundle.putString("MessageFromMainActivity",strMyMessage);
//Intent created for calling child activity.
Intent messageIntent = new
Intent ( MainMessageTask.this.getApplicationContext() , ChildMessageTask.class );
// putting values of myBundle object to Intent
messageIntent.putExtras(myBundle);
// Calling child activity with taking "Parent-Child" concept in mind.
startActivityForResult(messageIntent, 0);
}// end of if block...
}// end of click event...
}// end of main activity...
* This is a Main activity that demonstrates the Explicit Intent concept.
*
*/
public class MainMessageTask extends Activity implements OnClickListener {
private Button mySendMessage;
private EditText myMessage;
private TextView displayMessageFromChildScreen;
//Methods
/**
* onActivityResult - called when the child activity finishes its execution after calling finish method from child activity...
* This is where you should get data from previous activity [from child activity]
*
* Always called by child activity after calling finish method from ChildMessageTask...
* @param requestCode int
* @param resultCode int
* @param data Intent
*/
@Override
public void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
displayMessageFromChildScreen =
(TextView) findViewById (R.id. displayMessageFromChildActivity);
String Message= data.getStringExtra("BackResultToMainActivity");
displayMessageFromChildScreen.setText("Message from Child Activity :- " + Message);
}
}
/**
* onCreate - called when the activity is first created.
* This is where you should do all of your normal static set up: create views, bind data to lists, etc.
*
* Always followed by onStart().
*
*/
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mmt);
myMessage= (EditText) findViewById(R.id.Messages);
mySendMessage= (Button) findViewById(R.id.SendMessage);
mySendMessage.setOnClickListener(this);
}//end of onCreate method...
/**
* onClick - call when the "Send Message to Child Activity" button clicked.
* This is where you should do all of intent calling procedure.
*
*/
@Override
public void onClick(View button) {
String strMyMessage=myMessage.getText().toString();
myMessage.setText("");
if (button = = mySendMessage){
// setting up myBundle object for storing messages.
Bundle myBundle=new Bundle();
myBundle.putString("MessageFromMainActivity",strMyMessage);
//Intent created for calling child activity.
Intent messageIntent = new
Intent ( MainMessageTask.this.getApplicationContext() , ChildMessageTask.class );
// putting values of myBundle object to Intent
messageIntent.putExtras(myBundle);
// Calling child activity with taking "Parent-Child" concept in mind.
startActivityForResult(messageIntent, 0);
}// end of if block...
}// end of click event...
}// end of main activity...
"Code for Main Activity ChildMessageTask.class"
package com.helloworkshop.intentdemo;
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;
import android.widget.EditText;
import android.widget.TextView;
/**
* This is a Child activity that demonstrates the Explicit Intent concept.
* After finishing its execution child sent result back to main to onActivityResult method.
*/
public class ChildMessageTask extends Activity implements OnClickListener {
private EditText childMessage;
private Button repalyToMainScreen;
private TextView displayMainActivityResult;
/**
* onCreate - called when the activity is first created.
* This is where you should do all of your normal static set up: create views, bind data to lists, etc.
*
* Always followed by onStart().
*
*/
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.cmt);
displayMainActivityResult = (TextView) findViewById(R.id.displayMainMessage);
childMessage =(EditText) findViewById(R.id.replayToMainMessage);
repalyToMainScreen = (Button) findViewById(R.id.replayToMainScreen);
//this code is getting values from MainScreen Intent...
//here we specifying bundle and for getting values we uses same name as we,
//applied in previous bundle name "MessageFromMainActivity".
Bundle myBundle=this.getIntent().getExtras();
String MessageFromMainActivity= myBundle.getString("MessageFromMainActivity");
displayMainActivityResult.setText("Message from MainScreen :- " + MessageFromMainActivity );
repalyToMainScreen.setOnClickListener(this);
} // end of onCreate()...
/**
* onClick - call when the "Replay to Main Screen" button clicked.
* This is where you should do all of intent calling procedure.
*
*/
@Override
public void onClick(View replayToMainMessage) {
String childScreenMessage = childMessage.getText().toString();
childMessage.setText("");
if (replayToMainMessage = = repalyToMainScreen){
Bundle myBundle=new Bundle();
myBundle.putString("BackResultToMainActivity", childScreenMessage);
Intent intt=new Intent();
intt.putExtras(myBundle);
setResult(RESULT_OK,intt); // setting Result_ok constant so when child finish its execution,
// parent gets response from this.
this.finish(); // uses for finishing child activity and get back to main activity by system stack...
}//end of if block...
}// end of onClick()...
}// end of activity class...
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;
import android.widget.EditText;
import android.widget.TextView;
/**
* This is a Child activity that demonstrates the Explicit Intent concept.
* After finishing its execution child sent result back to main to onActivityResult method.
*/
public class ChildMessageTask extends Activity implements OnClickListener {
private EditText childMessage;
private Button repalyToMainScreen;
private TextView displayMainActivityResult;
/**
* onCreate - called when the activity is first created.
* This is where you should do all of your normal static set up: create views, bind data to lists, etc.
*
* Always followed by onStart().
*
*/
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.cmt);
displayMainActivityResult = (TextView) findViewById(R.id.displayMainMessage);
childMessage =(EditText) findViewById(R.id.replayToMainMessage);
repalyToMainScreen = (Button) findViewById(R.id.replayToMainScreen);
//this code is getting values from MainScreen Intent...
//here we specifying bundle and for getting values we uses same name as we,
//applied in previous bundle name "MessageFromMainActivity".
Bundle myBundle=this.getIntent().getExtras();
String MessageFromMainActivity= myBundle.getString("MessageFromMainActivity");
displayMainActivityResult.setText("Message from MainScreen :- " + MessageFromMainActivity );
repalyToMainScreen.setOnClickListener(this);
} // end of onCreate()...
/**
* onClick - call when the "Replay to Main Screen" button clicked.
* This is where you should do all of intent calling procedure.
*
*/
@Override
public void onClick(View replayToMainMessage) {
String childScreenMessage = childMessage.getText().toString();
childMessage.setText("");
if (replayToMainMessage = = repalyToMainScreen){
Bundle myBundle=new Bundle();
myBundle.putString("BackResultToMainActivity", childScreenMessage);
Intent intt=new Intent();
intt.putExtras(myBundle);
setResult(RESULT_OK,intt); // setting Result_ok constant so when child finish its execution,
// parent gets response from this.
this.finish(); // uses for finishing child activity and get back to main activity by system stack...
}//end of if block...
}// end of onClick()...
}// end of activity class...
- Here we also need to set up some of the things in "AndroidMenifest.xml file" ... which is given below...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helloworkshop.intentdemo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.helloworkshop.intentdemo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainMessageTask" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<activity android:name=".MainMessageTask" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<activity android:name=".ChildMessageTask" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
</application>
</manifest>
- What is <intent-filter> ?
- It is indicating or specifying action on activity which we will going to perform on activity.
- Specifying data on which we can perform our operations.
- all of our activities need to register in AndroidMenifest.xml file. First activity is going to be launched is "MainMessageTask". so that in <intent-filter> we specify <action> as a MAIN and <category> as LAUNCHER. So, by default android system is starting our MainMessageTask, activity firstly. using system handled intent.
- second activity in our app is "ChildMessageTask". so for that in <intent-filter> we specify <action> as a MAIN and <category> as DEFAULT.
- and XML layout is simple as mentioned in above figures 1 to 3.
- It is loosely couples our application architecture.
- It is mainly used for Activate android systems major component like Activity, Services, and also BroadCastReceivers.
- Using intent we can start another activity which is already design in our application architecture.
- We can also share data from across the system from application to application using Intent.