Register Login

Explicit and Implicit intent in Android

Updated May 23, 2019

Explicit intent is one that you use to launch a specific app component, such as a particular activity or service in your app.An implicit intent specifies an action that can invoke any app on the device able to perform action.

How to use explicit and implicit intent in a device

Please follow the steps below to create explicit intent:

Step 1: First you need to create a new app by clicking on the option “Start a new project”

Name the app as “AppOne”.

Next, you need to select the option “blank activity”

A new project is now created.

Step 2: You have to click on the “large text” option and drag the pointer on the default screen.

Step 3: you have to click on the “button” option and drag the pointer on the default screen.

Step 4: go to the text area to change the text of the “large text” to “Main Activity App One”

Step 5: Under “Button” section, change the text of the “Button” to “Open Activity One”

Now you need to create new an activity which will open when you click on the button “Open Activity One”.

Step 6: In order to create a new activity, click on the option “app”, then select the option “New”, select option “Activity” and finally select the option “Blank Activity”.

Step 7: you have to name this activity as “ActivityOneAppOne”

Step 8: Delete the text “hello world” and replace it in the text area “Activity One App One”

Step 9: You need to go to the mainactivity.xml file to open this activity.

Step 10: Next go to the “button” component and add an attribute called “android: onClick” and then you name a method for example “openOne”.

Step 11: You need to create this method in your mainactivity.java file

Step 12: here you create a method which will be “public void openOne”. Remember that here you use the same name as the method name. Put an argument adjacent to the method which is “View view” and inside this method create an instance “Intend_intend = new Intend”. Place two arguments. The first argument is the context “this” and second is the activity class name “ActivityOneAppOne.class”. This will create an intent object which you can call from the method called start activity. So write “start activity” and pass the intent object adjacent to it like this start activity (intent);” this is going to open your first activity using your main activity.

Now when you run this program and click on the button “Open Activity One”, it is going to open the Activity One in App One “Activity One App One”.

This was an example of explicit intent where you can open an activity within an app.

Please follow the steps below to create implicit intent:

Step 1: first you need to create a new app and name it as AppTwo

Your AppTwo is created

Step 2: here you design the app in order to open the activity from your first app – AppOne. For that, you have to click on the “button” option and drag the pointer on the default screen.

Step 3: go to the text area and change the text from “button” to “Open Activity One from App One”

Step 4: To open activity one from app one using your app two, first you need to go to your last project and open manifestfile.xml file.

Whenever you create an activity, you will find that some extra tags are created for your activity.

In order to be able to open this activity from your other app, you need to copy the filter intent from the main activity and paste it inside the activity as shown below.

Step 5: you need to change the android name of the two tags –“action” and “category” – in the activity area. In the action tag you need to give the android name of your activity. To do so, you need to go the activity one in-app one. Go the activity_activity_one_app_one.xml file of the activity one in app one and copy the package name form this section.

Now you need to paste this package name as your activity under the “action” tag. Pasting the package name in this section will enable your app to be visible on every app of your device.

Step 6: In order to allow other device to access your activity you have to have the “category” as “DEFAULT. Now your activity is visible to other apps within your device.

Step 7: Now go back to your AppTwo and enable this “button” which we have already created in AppTwo to open the activity from app one. To do this you need to go the text area and add an attribute under the button section. The attribute will be: android: onClick= “openAct1FromApp1”.



Now go to the method name option “openAct1FromApp1” and go to the Main Activity.java file of AppTwo. Create a new method “public void openAct1FromApp1 (View view)”. Then create an intent inside the method “intent intent = new Intent (); and paste the package name of AppOne instead of giving a specific context in the bracket of the method. Now write start activity and pas on the intent here “start activity (intent).

Once you are done with this, you can run your app.

Now when you click this “Open Activity One From App One” you will see that “Activity One App One” is opened.

This was an example of an implicit intent where you open an activity from some other app within your device.

Read Next About How to add Up button in Action Bar

Complete Code for Explicit Intent

app>java>com.example.programmingknowloedge.appone>MainActivity.java

package com.example.programmingknowloedge.appone;
import ...
public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void openOne(View view){
        Intent intent = new Intent(this, ActivityOneAppOne.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

}

app>manifest>AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns="http://schemas.android.com/apk/res/android"
    package="com.example.programmingknowledge.appone">
    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="AppOne"
            android:theme="@style/AppTheme">
            <activity
                    android:name=".MainActivity"
                    android:label="AppOne">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".ActivityOneAppOne"
                android:label="ActivityOneAppOne">
                <intent-filter>
                    <action android:name="com.example.programmingknowledge.appone.ActivityOneAppOne" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>       
    </application>
</manifest>

app>res>layout>activity_activity_one_app_one.xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout 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:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context="com.example.programmingknowledge.appone.ActivityOneAppOne">
    <textView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearence="?android:attr/textAppearenceLarge"
        android:text="Activity One App One"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="90dp"
        android:layout_marginStart="90dp"
        android:layout_marginTop="58dp"
        android:onClick="openOne"    
         />
</RelativeLayout>

app>res>layout>activity_main.xml

<?xml version="1.0" encoding="UTF-8"?>

<RelativeLayout 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:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".MainActivity"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textApperence="?android:attr/textApperenceLarge"
        android:text="Main Activity App One"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="88dp"
        android:layout_marginStart="88dp"
        android:layout_marginTop="47dp" />

    <Button
        android:layout_weight="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Activity One"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_alignLeft="@+id/textView"
        android:layout_alignStart="@+id/textView"
        android:layout_marginTop="36dp"/>
</RelativeLayout>


×