Android Explicit Intents with Examples

  Total Views : 222
  Comments : 0
  Keywords : Android explicit intents are used to invoke specific app components such as activity or service of an app, Android explicit intents to start components in same application

Here we will learn explicit intents in android with examples and how to use android explicit intents to start an activity or service and send an information from one activity to another activity with examples.

Android Explicit Intents

In android, Explicit intents explicitly specify the name of component to be invoked by an activity and we use explicit intents to start a component in our own app. For example we can start a new activity in response to a user action using explicit intents.

 

By using explicit intents we can send or share data / content from one activity to another activity based on our requirements. To create an Explicit Intent, we need to define the component name for Intent object.

 

Following is the simple code snippet of explicit intent in android application.

 

Intent di = new Intent(this, ActivityView.class);
di.setData(Uri.parse(
"http://www.tutlane.com"));
startService(di);

If you observe above explicit intent we specified an app context and external class name (ActivityView) object. The intent explicitly start the ActivityView class in the app.

Android Explicit Intent Example

Following is the complete example of implementing an explicit intent in android application. Here we will do addition of two numbers in one activity and sending that information to another activity to display the result.

 

Create a new android application using android studio and open an activity_main.xml file from \src\main\res\layout path. In case if you are not aware of creating an app in android studio check this article Android Hello World App.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical" android:layout_width="match_parent"
   
android:layout_height="match_parent">
    <
TextView
       
android:id="@+id/fstTxt"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="100dp"
       
android:layout_marginTop="150dp"
       
android:text="First Number"
        
/>
    <
EditText
       
android:id="@+id/firstNum"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="100dp"
       
android:ems="10">
    </
EditText>
    <
TextView
       
android:id="@+id/secTxt"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Second Number"
       
android:layout_marginLeft="100dp"
        
/>
    <
EditText
       
android:id="@+id/secondNum"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="100dp"
       
android:ems="10" />
    <
Button
       
android:id="@+id/addBtn"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_marginLeft="100dp"
       
android:text="Add" />
</
LinearLayout>

Now we will create another layout resource file result.xml in \src\main\res\layout path to get the first activity (activity_main.xml) details in second activity file for that right click on your layout folder à Go to New à select Layout Resource File and give name as result.xml.

 

Once we create a new layout resource file result.xml, open it and write the code like as shown below

result.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:orientation="vertical" android:layout_width="match_parent"
   
android:layout_height="match_parent">
<
TextView
   
android:layout_width="wrap_content"
   
android:layout_height="wrap_content"
   
android:id="@+id/resultView"
   
android:layout_marginLeft="100dp"
   
android:layout_marginTop="150dp"/>
</
LinearLayout>

Now open our main activity file MainActivity.java from \src\main\java\com.tutlane.intents path and write the code like as shown below

MainActivity.java

package com.tutlane.intents;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
final EditText firstNum = (EditText)findViewById(R.id.firstNum);
       
final EditText secNum = (EditText)findViewById(R.id.secondNum);
        Button btnAdd = (Button)findViewById(R.id.
addBtn);
        btnAdd.setOnClickListener(
new View.OnClickListener() {
           
@Override
           
public void onClick(View v) {
               
int num1 = Integer.parseInt(firstNum.getText().toString());
               
int num2 = Integer.parseInt(secNum.getText().toString());
                Intent intent =
new Intent(MainActivity.this,ResultActivity.class);
                intent.putExtra(
"SUM",num1+" + "+num2+" = "+(num1+num2));
                startActivity(intent);
            }
        });
    }
}

Now we will create another activity file ResultActivity.java in \src\main\java\com.tutlane.intents path to get the first activity (MainActivity.java) details in second activity file for that right click on your application folder à Go to New à select Java Class and give name as ResultActivity.java.

 

Once we create a new activity file ResultActivity.java, open it and write the code like as shown below

ResultActivity.java

package com.tutlane.intents;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

/**
 * Created by surdasari on 27-07-2017.
 */

public class ResultActivity  extends AppCompatActivity {
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
result);
        TextView result = (TextView)findViewById(R.id.
resultView);
        Intent intent = getIntent();
        String addition = (String)intent.getSerializableExtra(
"SUM");
        result.setText(addition);
    }
}

Now we need to add this newly created activity in ActivityManifest.xml file in like as shown below.

ActivityMainfest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   
package="com.tutlane.intents">
    <
application
       
android:allowBackup="true"
       
android:icon="@mipmap/ic_launcher"
       
android:label="Explicit Intent - Activity1"
       
android:roundIcon="@mipmap/ic_launcher_round"
       
android:supportsRtl="true"
       
android:theme="@style/AppTheme">
        <
activity android:name=".MainActivity">
            <
intent-filter>
                <
action android:name="android.intent.action.MAIN" />
                <
category android:name="android.intent.category.LAUNCHER" />
            </
intent-filter>
        </
activity>
        <
activity android:name=".ResultActivity" android:label="Explicit Intent - Activity2">
        </
activity>
    </
application>
</
manifest>

If you observe above example we are performing an addition operation in one activity (MainActivity.java) and sending those details to another activity (ResultActivity.java) and added all the activities in AndroidManifest.xml file.

Output of Android Explicit Intent Example

When we run above example in android emulator we will get a result like as shown below

 

Android Explicit Intent Example Result or Output

 

This is how we can use Android Explicit Intents to send an information from one activity to another activity based on our requirements.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User