Blogger news

Blogger templates

Friday, May 4, 2012

Android Intents

1. Android Intents

1.1. Overview

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.
Therefore Intents allow to combine loosely coupled components to perform certain tasks.
Intents can be used to signal to the Android system that a certain event has occurred. Other components in Android can register to this event and will get notified.
Intents are instances of the android.content.Intent class.
Intents are send to the Android system. Depending on how the Intent was constructed the Android system will run an receiver determination and determine what to do.
An Intent can also contain data. This data can be used by the receiving component. For example your application can calls via an Intent a browser component. As data is it may send the URL to the browser component.
Android supports explicit and implicit Intents.

1.2. Explicit Intents

Explicit Intents explicitly names the component which should be called by the Android system, by using the Java class as identifier.
The following shows an explicit Intent. If that Intent is correctly send to the Android system, it will start the associated class.

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo");

Explicit Intents are typically used within on application as the classes in an application are controlled by the application developer.

1.3. Implicit Intents

Implicit Intents do not specify the Java class which should be called. They specify the action which should be performed and optionally an URI which should be used for this action.
For example the following tells the Android system to view a webpage. Typically the web browser is registered to this Intent but other component could also register themself to this event.

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(""));


If these Intents are send to the Android system it searches for all components which are registered for the specific action and the data type.
If only one component is found, Android starts this component directly. If several components are identifier by the Android system, the user will get an selection dialog and can decide which component should be used for the Intent.

1.4. Data Transfer

An implicit Intent contains the Action and optional the URI. The receiving component can get this information via the getAction() and getData() methods.
Explicit and implicit Intents can also contain additional data. This data call be filled by the component which creates the Intent. It can and can get extracted by the component which receives the Intent.
The component which creates the Intent can add data to it via the overloaded putExtra() method. Extras are key/value pairs; the key is always a String. As value you can use the primitive data types (int, float,..), String, Bundle, Parceable and Serializable.
For example you can trigger all components which have been registered to send some data via the new Intent(Intent.ACTION_SEND) This Intent determines possible receivers via the type. What is send it defined via the putExtra method. You can use any String as key, the following uses the keys which are predefined for the ACTION_SEND intent.

Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "News for you!");
// createChooser is a convenience method to create
// an Chooser Intent with a Title
startActivity(Intent.createChooser(sharingIntent,"Share this using"));

The component which receives the Intent can use the getIntent().getExtras() method call to get the extra data.

Bundle extras = getIntent().getExtras();
if (extras == null) {
// Get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
 // Do something with the data

2. Using Intents to call Activities

2.1. Calling Activities

If you send an Intent to the Android system, Android requires that you tell it to which type of component your Intent should be send.
To start an Activity use the method startActivity(Intent). This method is defined on theContext object and available in every Activity object.
If you call an Activity with the startActivity(Intent) method the caller requires no result from the called Activity.

2.2. Calling Sub-Activities for result data

If you need some information from the called Activity use the startActivityForResult() method.

public void onClick(View view) {
 Intent i = new Intent(this, ActivityTwo.class);
 i.putExtra("Value1", "This value one for ActivityTwo ");
 i.putExtra("Value2", "This value two ActivityTwo");
 // Set the request code to any code you like, you can identify the
 // callback via this code
 startActivityForResult(i, REQUEST_CODE);


If you use the startActivityForResult() method then the started Activity is called a Sub-Activity.
If the Sub-Activity is finished it can send data back to its caller via Intent. This is done in the finish()method.

public void finish() {
 // Prepare data intent 
 Intent data = new Intent();
 data.putExtra("returnKey1", "Swinging on a star. ");
 data.putExtra("returnKey2", "You could be better then you are. ");
 // Activity finished ok, return the data
 setResult(RESULT_OK, data);

Once the Sub-Activity finished, the onActivityResult() method in the calling Activity will be called.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (resultCode == RESULT_OK && requestCode == REQUEST_CODE) {
  if (data.hasExtra("returnKey1")) {
   Toast.makeText(this, data.getExtras().getString("returnKey1"),


Sonu M said...

Nice Stuff:) Very Simple and Perfect

Vijaynath said...

Thank you Sonu :)

Post a Comment