Access an activity using explicit intents in Android

June 22, 2012

In simple terms an intent is an object used to access a component from an activity. The component can be an activity or a service or a broadcast receiver. We can classify intents into two category such as explicit intents and implicit intents. In the case of explicit intent, the target component is accessed by its class name. On the other hand, in the case of implicit intent, the target component is accessed by action name, category and data. In this article we are going to create an application which access a target activity using explicit intent.

1. Create an Android project namely ExplicitIntentDemo

New Android Project

Figure 1 : New Android Project

2. Select a build target for this application

Select Android build target

Figure 2: Select Android build target


3. Enter application details

Enter application Details

Figure 3: Enter application Details

4. Open the file res/values/strings.xml for adding new strings namely str_second_activity and str_btn_caption. After adding these strings, the file will look like as follows :

<?xml version="1.0" encoding="utf-8"?>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">ExplicitIntentDemo</string>
    <string name="str_second_activity">Second Activity</string>
    <string name="str_btn_caption">Access Second Activity</string>

5. Create a layout file for the second activity. For this, create an Android XML file called second.xml in the directory res/layout/. Then open and update the file with the given below code:

<?xml version="1.0" encoding="utf-8"?>
    android:orientation="vertical" >

6. Create an activity class for the second activity. For this, create a class file called in the directory src/in/wptrafficanalyzer/explicitintentdemo and update its content with the given below code

package in.wptrafficanalyzer.explicitintentdemo;
import android.os.Bundle;
public class SecondActivity extends Activity{
    protected void onCreate(Bundle savedInstanceState) {

7. Open the file AndroidManifest.xml for adding the newly created activity component to the application. Since this activity is accessed via an explicit intent, there is no need to define any intent filters for this activity. Because explicit intents are not being watched by any intent filters.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

        android:label="@string/app_name" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            android:label="@string/app_name" >

8. Open the layout file of the MainActivity ( res/layout/main.xml ) for adding new button. Clicking on this button will show the SecondActivity which we have created above. The updated main.xml file is given below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >

        android:text="@string/str_btn_caption" />

9. Create an explicit intent in the MainAcitivity. For this, open and update the file /src/in/wptrafficanalyzer/explicitintentdemo/ with the given below code

package in.wptrafficanalyzer.explicitintentdemo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */

    public void onCreate(Bundle savedInstanceState) {

        /** Getting a reference to the button in the main.xml **/
        Button btn = (Button) findViewById(;

        /** Defining a click listener for the button **/
        OnClickListener btnClickListener = new OnClickListener() {
            public void onClick(View v) {

                /** Creating an explicit intent to access the SecondActivity **/
                Intent intent = new Intent(getBaseContext(), SecondActivity.class);

                /** Starting the SecondActivity **/


10. Execute the application


Figure 4: MainActivity


Figure 5: SecondActivity

11. Download the source code

12. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner