Intent In Android Studio With Examples | Full Explanation

Intent in android studio is a mechanism that passes data between activities. And also used for broadcast services in android devices. Using intent we call another activity and can use the device services like sending mail, message, receiving message, getting OTP from message, opening camera. There are many services that we will discuss further.

intent in android studio
intent in android studio

Use of Intent and Intent Filters In Android Studio

Intent has the following features in android studio.

  1. Start an Activity
  2. Launch the App
  3. Start a Service
  4. Delivering the Broadcast

1. Start an Activity Using Intent In Android Studio

Intent starts an Activity by calling startActivity() method. You can move to another class or screen using intent service in android studio. Below is the example of calling startActivity() method.

Calling another Activity from an Activity in Android

To call another Activity from an Activity use the following code. You can use this keyword or that class name in the first argument as follows.

 startActivity(new Intent(this, SecondClass.class));

or

 startActivity(new Intent(FirstClass.this, BottomNavigationActivity.class));

How to Call an Activity from Fragment in Android Studio

To call an Activity from Fragment, use getActivity() or context in Adapter class in android.

 startActivity(new Intent(getActivity(), BottomNavigationActivity.class));

How to Pass data to Intent in Android Studio

To pass data into another class using intent , the putExtra() method is used. It takes two string values, first one is key and another one is value. You can create an object of Intent or directly use startActivity(Intent).putExtra(), I created an object of Intent. Pass data in intent like as follow.

 Intent intent = new Intent(this, SecondClass.class);
 intent.putExtra("key_name", "value");
 startActivity(intent);

Note : Use context.startActivity() in adapter class to call an intent. Create context if you not have in adapter class.

To get values from intent into another class, the getIntent() and getStringExtra () method is used. Example is below.

 Intent intent = getIntent();
 String name = intent.getStringExtra("key_name");

Put the name of the key that you create in the first argument of the putExtra() method while calling the other class. Here I am receiving the value into a string as above.

2. Launch An Activity Using Intent Filter

To launch an Activity when the app starts, Intent Filter is used. Generally we create a Splash activity which launches when the app starts. For this Intent Filters are used in Manifiest.java class in android studio. You can use Intent Filters as below in Manifiest.java file for launch and Activity. You have to create an intent filter inside the <activity > </activity> tag.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.AndroidTestingProject">
        <activity
            android:name=".Activity.ProductDetailsActivity"
            android:exported="true" />
        <activity
            android:name=".Activity.BottomNavigationActivity"
            android:exported="true" />
        <activity android:name=".Activity.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3. Start a Service

The Service is a component in android studio that performs operation in background without user interface. Like downloading anything from the server

After Android 5.0 you can start a Service with the help of JobScheduler. And for Version earlier than 5.0 you can start a Service with the help of Service class.

You can bind a service with another component if the service is designed with a client-server-interface. You can bind by passing an Intent to bindService().

4. Delivering A Broadcast

You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(). A broadcast is a message that any app can receive. The system delivers various broadcasts to system events such as when the system boots up and the device starts charging.

For example you can put your phone on Aeroplane mode that uses broadcasts. To use broadcast you have to declare the receiver in Manifiest.java file in android studio. For deep details you can visit developer google site.

Using broadcast you can receive system data like getting sms information form the device in. Broadcast receivers are of two types.

  1. Static Broadcast Receivers – These types broadcast receiver declared in manifest file and works even when the app is closed.
  2. Dynamic Broadcast Receivers – These types of broadcast receivers work only when the app is running.

Types of Intent in Android Studio

There are two types of intent.

  1. Explicit Intent
  2. Implicit Intent

1. Explicit Intent

Using Explicit Intent you can call another Activity and pass some data in intent. These intents refers to any class and available in package. These are handled by Activities in android studio. You can specify the start and target Activity in this. Example is below.

 startActivity(new Intent(MainActivity.this, ProductDetailsActivity.class));

2. Implicit Intent

In the Implicit Intent no component is specified. These Intents perform some action from the system. Like you can load webview by passing URL in intent action, you can send email, send message, can call using implicit intent in android studio as below.

Loading Webpage using Implicit Intent in Android Studio

Pass the URL of webpage into to URL.

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
 startActivity(intent);

Sending Email Using Implicit Intent In Android Studio

 Intent email = new Intent(Intent.ACTION_SEND);
 email.putExtra(Intent.EXTRA_EMAIL, new String[]{"To Email"});
 email.putExtra(Intent.EXTRA_SUBJECT, "Write Subject");
 email.putExtra(Intent.EXTRA_TEXT, "Write message");
 startActivity(Intent.createChooser(email, "Choose Email client"));

You can perform many actions using intent in android studio like opening camera, device storage and much more things.

Conclusion

In this tutorial we learnt that Intent and intent filters in android studio are used to perform some action. It can be calling another activity or using system services. All these services are a part of intent. Hope you learnt something from this tutorial. Please comment on our motivation.

You can learn more Android and Flutter tutorials for growing your skill.

Suggestions:

Map in Java Full Explanation

Linear Layout In Android Studio

Bottom Sheet in Android

Relative Layout in Android Studio With Examples

Timer in Android

How to generate android debug apk for testing the app

CardView In Android Studio With Examples

Spinner in Android

Android Splash Screen Full Example

SQLITE Database In Android Studio Example

GridLayout in android

What is WebView In Android Studio

Pull To Refresh In Android Studio

Opening Camera In Android Studio Examples

Tabs In Android Studio Example

Intent In Android Studio With Examples

Creating Rounded Corner Button In Android Studio

Full Screen VideoView In Android Studio Example

Auto Image Slider In Android Studio Example

Recyclerview in android Complete Example

Android DataBinding Example | Enable Data Binding in Android

Bottom Navigation Bar In Android

Navigation Drawer In Android Studio With Example | FlutterTPoint

Why String are immutable in Java Full Explanation

Donโ€™t miss new tips!

We donโ€™t spam! Read our [link]privacy policy[/link] for more info.

Leave a Comment

Translate ยป
Scroll to Top