Saturday 14 March 2015

Understanding Explicit and Implicit intents in Android

Background

Intents form the very basics of Android operating Systems. Primarily they are used to start new activities and transfer data between activities. There are two broad categories in which Intents can be classified - 
  1. Explicit Intents
  2. Implicit Intents
In Explicit intents we specify which exact Activity need to be started by giving the class of the Activity to be started. Android OS will start that exact Activity.

Implicit intents on the other hand behave differently. In implicit intents you provide various parameters like action, category, data etc. Activity that supports these parameters will be started by Android OS. For eg. lets say we provide "view" action which is the action to view a web page in browser. Android OS will see a browser (lets say chrome) supports this action and will start it. But wait a minute - what happens when multiple activities support these parameters. Like when we have multiple browsers installed. In this case Android OS will simply give option to the user to choose from list of Activities that support those combination of parameters.




We will see these is detail a little later. But I hope you got the overview of the basic difference between explicit and implicit intents.

Explicit Intents

As mentioned earlier explicit intents are used to start a specific Activity directly. Lets create two activities 
  1. PrimaryActivity and
  2. SecondaryActivity
Primary Activity will have a button which on press will start Secondary Activity using an explicit intent.

I am going to skip contents of other files like manifest file or resources file as aim of this post is to demonstrate types of intents. If you wish to learn resources and other folder that constitutes an Android project see one of my previous posts -

PrimaryActivity.java :

 package com.example.explicitintentdemo;

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

public class PrimaryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_primary);
    
        Button explicitIntentButton = (Button) findViewById(R.id.explicit_intent_button);
        explicitIntentButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent explicitIntent = new Intent(PrimaryActivity.this, SecondaryActivity.class);

                // explicit intent
                startActivity(explicitIntent);
            }
        });
        
    }
}


SecondaryActivity.java :

package com.example.explicitintentdemo;

import android.app.Activity;
import android.os.Bundle;

public class SecondaryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_secondary);
    }
}



You can test it on your Android device or emulator. Pressing on "Start Secondary Activity" loads the secondary Activity.









So when the button is pressed we create a intent with Application context and class of Activity that needs to be started (Explicit intent). Then we simply start the activity using startActivity() method.



Now lets see how implicit intents are used.

Implicit Intents

As mentioned earlier implicit intents are kind of generic intents that are used to fire up Activities supporting some action. For example activities that can understand web page URLs and render them (typically browsers). As we know there can be many such activities (many browsers installed or same android OS for instance). In this case Android OS lets user decide which activity should be started. 

Lets see this in code. We will try to open simple google URL - "http://www.google.com" using proper intent.

Lets refactor our Primary Activity to use implicit intent and render the google URL.

PrimaryActivity.java :

package com.example.explicitintentdemo;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class PrimaryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_primary);
    
        Button explicitIntentButton = (Button) findViewById(R.id.explicit_intent_button);
        explicitIntentButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent baseIntent =  new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
                Intent chooserIntent = Intent.createChooser(baseIntent, "Choose your browser");
                startActivity(chooserIntent);
            }
        });
        
    }
}

Here the logic in onClick() method is slightly changed. Instead of using an Intent with Application Context and the class of Activity to be started we are providing it with an Action (Intent.ACTION_VIEW) and a data which is essentially an URI.

Note : Think of chooserIntent as a wrapper Intent. When the dialog is shown to choose from multiple activities that support the action (category, data etc) the title text  can be supplied using such Chooser Intents [See the second argument of Intent.createChooser() method call.]. 

Lets see this in action.

UI for PrimaryActivity is the same. Click on the "StartSecondaryActivity" to launch implicit intent.




Notice the title of the chooser dialog ? It is the same String we supplied in Chooser intent - 
  • Intent chooserIntent = Intent.createChooser(baseIntent, "Choose your browser");
I hope you got the difference between the two - implicit and explicit intents. If you are wondering what is the action (Intent.ACTION_VIEW) and the data that we supplied are and how intents are related see following documentation links.

After going through the documentation if you are wondering how intent- filter would look for the Activity that supports viewing URL like the one in above example then it is as follows -

        <activity
            android:name=".MyBrowserActivity"
            android:label="@string/my_app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="http"/> 
              </intent-filter>
        </activity>


Infact the MyBrowser Activity you saw in chooser list is one of the browser like demo app that I had created.
 So to give summary about intent filters - they define what kind of operation the Activity is capable of handling and is specified in the Applications manifest file inside the respective activity tag. If you would recollect starting activity of each application have intent filters like -

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>


These intent filters tell Android OS that this activity is a launcher activity.

Note : I have used startActivity() everywhere to start new Activity but if you want your new started activity to return some data/result to the starter activity you can use startActivityForResult() method. For more details on that you can refer -

Related Links

t> UA-39527780-1 back to top