All about Intents

Dublin Buzz uses the Android Intent system to either launch other internal activities (e.g. when viewing a sight) or launching external ones (e.g. launching a web browser). Intents can be thought of as a very general, flexible, dynamic messaging system. Activities can be very loosely coupled, meaning that you just declare that you want some other activity to handle some action for you and – little bit of magic dust sprinkled here – it launches.
Okay, it ain’t quite that easy. But it is certainly straightforward.

I’m going to show you how I use internal activities in my Dublin Buzz application. For that, you’ll need to look at the code in SightViewer.java, available as a text file here: SightViewer. (You cannot append .java files in WordPress, so you’ll have to cut-and-paste it into your favourite editor…).

Let’s start by looking at the click event handler in Dublin Buzz. This is where the activity is launched from. (A user has clicked a sight button, say Kilmanham Jail).

v.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
    	Intent launch = new Intent(c, SightViewer.class);
    	launch.putExtra("Sight", s);
    	startActivity(launch);
}});

The variable “v” (I know, bad name…) refers to the Button that was pressed, and was declared earlier. setOnClickListener assigns a method to be the listener for click events. I am using Java’s anonymous class mechanism to declare this method in-line. This saves some time and space, but can be a little odd if you’re seeing it for the first time. But just focus on the lines starting at:

    public void onClick(View arg0) {

There are three steps (in this specific example) to launching an activity via an intent: Step 1 creates an object to encapsulate the intent, Step 2 bundles up some data for the intent to use, and Step 3 actually launches the intent.

The first Step is where I declare my intention to launch an explicitly-named activity:

    Intent launch = new Intent(c, SightViewer.class);

The “c” variable is the current context, that is, this activity itself. It had been declared earlier using this simple line of code:

    final Context c = this;

The second step is to send some data to the called Intent, to allow it to display a Sight.

launch.putExtra(“Sight”, s);

Data being sent to an Intent is called an “Extra” in Android-speak. All extras a bundled into the intent using key/value pairs. In this case, “Sight” is the key, and the data is the current Sight, “s” (I love short, simple names. If this sends a shiver down your spine, I apologise!).
Finally, you launch the activity by calling the startActivity() method:

startActivity(launch);

Now we need to look at the receiver. This is where the activity begins. First, it must unbundle the extra data sent to it (the Sight).

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

    Intent sight = this.getIntent();
    if (sight!=null)
    {
      	if (sight.hasExtra("Sight"))
      	    theSight = (Sight)sight.getSerializableExtra("Sight");
    }
}

This code – after the standard call to the superclasses’ onCreate() method and setting the content view, pulls the extras down. It retrieves the intent that launched it, checks that this intent has some extra data with the key “Sight”, and then assigns that extra data to the variable theSight, using the getSerializableExtra() method, passing in the relevant key.
To make this code work, of course, the application’s manifest must explicity declare the SightViewer activity as being capable of handling a View action.

  <activity android:name=".SightViewer"
        android:label="@string/app_name">
   <intent-filter>
       <action android:name="android.intent.action.VIEW" />
       <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
  </activity>

So next time you click a button on my Dublin Buzz application, you’ll know just exactly how “they” do that!

Advertisements
This entry was posted in Android, Android development, Mobile Apps. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s