Use Intent in Android Development

27 Oct 2013, 06:15 AM

Introduction

Intent is the asynchronous message which allows android components to request functionality from the other component of the Android System. It represents an app’s ‘Intent to do something’. We can use intents for different tasks e.g. making call, open map, open activity etc. But most often they are used to start another activity.

How to Start another Activity

Suppose you have another activity name “LifeCycleActivity” and its layout “activity_life_cycle.xml” in layout folder and It is also has necessary information of activity in “AndroidManifest.xml” file.

Create object of Intent class and pass it to the startActivity() method. Intent class has different constructor, for starting activity pass context and activity class to the object. Activity class is the subclass of context, so pass this as a first parameter and the pass the class as to which the system should deliver the intent. Then pass this intent to the startActivity() method and your second activity will be started.

 Intent intent = new Intent(this, LifeCycleActivity.class);
 startActivity(intent);

There are some other methods for starting activity. You can study about them too.

  • startActivityForResult(intent, requestCode)
  • startActivityFromChild(child, intent, requestCode)
  • startActivityFromFragment(fragment, intent, requestCode)

Open Phone Dialer

Launching the Phone dialer is also quite easy in Android you can send message to Android OS to run specific application using intent.

 Uri uri = Uri.parse("tel:03361122334");
 Intent intent = new Intent(Intent.ACTION_VIEW,uri);
 startActivity(intent);    

    

When you create asynchronous message (intents) from the Uri, tells the system to open activity that can handle the Uri scheme. In above example the Uri start from tel: loads an activity from the phone dialer application with the number loaded.

Pass data to another Activity

You can easily pass data with your asynchronous message (intent) as we passed to the phone dialer activity. But we passed to phone dialer activity using Uri. You can also put string, integer or other values in intent and you can also put your custom class object. Use intent.putExtra() method to put extra information in your intent. This method has many overloaded method, use appropriate method for your data.

 Student std = getStudent();
		
 Intent intent = new Intent(this, DetailActivity.class);
 intent.putExtra("student", (Serializable)std);
 startActivity(intent);

    

The activity “DetailActivity” that is receiving the intent exposes the method for retrieving the extra information. The getIntent() method returns the intent and then you can get extra information using getExtras() method. This method returns the bundle and from the bundle you can get your desired information using get methods as per your information type. In this example I am calling “(Student)bundle.getSerializable("student");” because my information was serialize able object and then I am casting it back to my concrete class object.

 Bundle bundle = this.getIntent().getExtras();
 Student std = (Student)bundle.getSerializable("student");
 String name = std.getName();

    
About Blog
This blog explains how to use Intent in Android application development.

Date 27 Oct 2013, 06:15 AM
Views 2,769
;