Sai Geetha started off as a Java Developer and moved on to work as a Solution Architect and an Enterprise Architect. However, as a hobby she continues to don the hats of a Java Developer as well as an Android Developer. She loves to share what she learns with the larger community. In her interest to share her knowledge, she blogs Android tutorials at saigeethamn.blogspot.com and her other technical musings at sageethatechnical.blogspot.com. Sai Geetha is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. You can read more from them at their website. View Full User Profile

Explicit Intent | Android Tutorial for Beginners (Part 2)

05.08.2011
| 12372 views |
  • submit to reddit
Having introduced you to the basic anatomy of an android application in the Part 1 of the series, I would like to show you an example where communication between 2 activities happens through an intent.

However, just one more detail to be introduced as promised and that is -
There are 2 types of intents that Android understands.
  1. Explicit Intent
  2. Implicit Intent

In an Explicit intent, you actually specify the activity that is required to respond to the intent. In other words, you explicitly designate the target component. This is typically used for application internal messages.

In an Implicit intent (the main power of the android design), you just declare an intent and leave it to the platform to find an activity that can respond to the intent. Here, you do not declare the target component and hence is typically used for activating components of other applications seamlessly

Note: Here for simplicity sake I tell an activity responds to an intent, it could as well be other types of components.

Now I will jump into the example which you can download from here:

This example has 2 activities:
  1. InvokingActivity
  2. InvokedActivity

The InvokingActivity has a button "Invoke Next Activity" which when clicked explicitly calls the "InvokedActivity" class.
The relevant part of the code is here:
  
        Button invokingButton = (Button)findViewById(R.id.invokebutton);
        invokingButton.setOnClickListener(new OnClickListener() {
         
         public void onClick(View v) {
          Intent explicitIntent = new Intent(InvokingActivity.this,InvokedActivity.class);
          startActivity(explicitIntent);
         }
        });

As explained in part 1 of the series, this is very much like an API call with compile time binding.

NOTE: The layout for InvokingActivity is defined in main.xml and for InvokedActivity in InvokedActivity.xml. The downloadable example can be opened in Eclipse Ganymede as an android project and can be executed.

In the next part of the series, we will see how to work with implicit intents which also needs us to understand intent-filters.

Reference:  http://saigeethamn.blogspot.com/2009/08/android-developer-tutorial-for.html
Published at DZone with permission of Sai Geetha M N, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)