Android Explicit Intent With Example

By | September 6, 2012
Intents are used as a message passing mechanism within the application and between the applications.  

An intent generically defines an “intention” to do some work. Intents encapsulate several concepts


You can use intents to invoke other applications from your application. You can use intents to invoke internal or external components from your application .

  Intents are used to perform the following tasks :-
  •    Start Activity to launch an Activity
  •    Broadcast Intent to send it to any interested Broadcast Receiver components. 
  •    Start Service to communicate with a background Service .intents ares always initiated by your application , An intent can be Explicit or implicit .

Explicit Intent: – 

In an explicit intent, we actually specify the activity that is required to respond to the intent. In other words, we explicitly designate the target component.

Lets see an example of launching an activity using explicit intent

Project Detail

Project Name Explicit_intent_demo
Package com.example.explicit_intent_demo
Minimum SDK API 8
Target SDK API 17
Theme Holo Light with Dark Action Bar

1. XML Layout

Create XML Files
1) screen1.xml
2) screen2.xml

file :- screen1.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout 
    xmlns:android=”http://schemas.android.com/apk/res/android”
    xmlns:tools=”http://schemas.android.com/tools”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”vertical” >

    <TextView
        android:id=”@+id/textView1″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_gravity=”center”
        android:gravity=”center”
        android:text=”IN SCREEN ONE”
        android:textAppearance=”?android:attr/textAppearanceLarge” />

    <Button
        android:id=”@+id/next_buttom_id”
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:text=”Next” />

</LinearLayout>

file:- screen2.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout 
     xmlns:android=”http://schemas.android.com/apk/res/android”
    android:layout_width=”match_parent”
    android:layout_height=”match_parent”
    android:orientation=”vertical” >

    <TextView
        android:id=”@+id/textView1″
        android:layout_width=”wrap_content”
        android:layout_height=”wrap_content”
        android:layout_gravity=”center”
        android:gravity=”center”
        android:text=”IN SCREEN TWO”
        android:textAppearance=”?android:attr/textAppearanceLarge” />

</LinearLayout>

2. Activities

Create Activity Classes
1) Explicit_intent
2) Next_Activity

Create a class Explicit_intent and extend it to activity class and override the onCreate() method of activity class and set the content of the activity with the above defined xml file by calling setContentView() method and passing xml file name as a parameter to it

setContentView(R.layout.screen1);  

And inside the Explicit_intent class set the onclick listener for button and create the intent , and pass the intent object to startActivity method as a paramter to it .

file:-Explicit_intent.java

package com.example.explicit_intent_demo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;

public class Explicit_intent extends Activity {

    Button next_button;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.screen1);
        next_button = (Button) findViewById(R.id.next_buttom_id);
        next_button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                // TODO Auto-generated method stub
                Intent explicit_intent = new Intent(Explicit_intent.this,
                        Next_Activity.class);
                startActivity(explicit_intent);
            }

        });
    }
}

Create a class Next_Activity and extend it to activity class and override the onCreate() method of activity class and set the content of the activity with the above defined xml file (screen2) by calling setContentView() method and passing xml file name as a parameter to it.

                                   setContentView(R.layout.screen2); 

file:-Next_Activity.java

package com.example.explicit_intent_demo;

import android.app.Activity;
import android.os.Bundle;
public class Next_Activity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
    }
}

Android Manifest

<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
    package=”com.example.explicit_intent_demo”
    android:versionCode=”1″
    android:versionName=”1.0″ >

    <uses-sdk
        android:minSdkVersion=”8″
        android:targetSdkVersion=”15″ />

    <application
        android:icon=”@drawable/ic_launcher”
        android:label=”@string/app_name”
        android:theme=”@style/AppTheme” >

        <!– Main Activity –>
        <activity
            android:name=”.Explicit_intent”
            android:label=”@string/title_activity_explicit_intent” >
            <intent-filter>
                <action android:name=”android.intent.action.MAIN” />
              <category android:name=”android.intent.category.LAUNCHER” />
            </intent-filter>
        </activity>
      
        <!– Next Activity –>
        <activity
             android:name=”.Next_Activity”
             android:label=”@string/app_name”  />
       
    </application>

</manifest>

3. RUN

  • @Smisha @Pavan
    Thank You

  • @kamakshi @smisha

    The error that you might be getting because of missing activity tag for second activity , you can find that in the above manifest file

  • This is the problem of your code. I think the bug is in the section that calls the second activity.

  • hi…
    wen i run dis only screen 1 runs and wen i click on next i gt an error as unfortunately the app closed…pls help me out to resolve dis

  • you forgot to add a new activity to AndroidManifest.xml.

  • now already android:targetSdkVersion="19"