EDDYMENS

Published 2 months ago

What Is An Intent In Android Development?

Table of contents

An Intent in Android is like a message. You use it to ask another part of the app to do something. For example, you can use an Intent to start a new screen, send a message, or talk to a background service. Intents are essential for these actions.

Types of Intents

There are two types of Intents: Explicit and Implicit.

Explicit Intents: You use these when you know exactly which part of the app you want to target. For example, if you want to go from one screen to another in the same app, you use an explicit intent.

01: Intent intent = new Intent(CurrentActivity.this, TargetActivity.class); 02: startActivity(intent);

Implicit Intents: You use these when you do not know the exact part of the app or when you want the system to choose. An implicit intent asks for a general action, and any app that can do that action can respond.

01: Intent intent = new Intent(Intent.ACTION_VIEW); 02: intent.setData(Uri.parse("http://www.example.com")); 03: startActivity(intent);

Since the data provided is a URL [→], startActivity is likely going to fire up a browser window.

Key Parts of an Intent

  • Action: The action to be done, like ACTION_VIEW, ACTION_SEND, or ACTION_MAIN.
  • Data: The data to work with, like a web link or an email address.
  • Category: Extra information about the action.
  • Extras: Extra data to pass to the target part.
  • Flags: Special instructions for the Intent, like how to start the activity.

Common Uses of Intents

Starting Activities: The most common use of an Intent is to start a new screen.

01: Intent intent = new Intent(this, NewActivity.class); 02: startActivity(intent);

Starting Services: You can also use Intents to start a service to run background tasks.

01: Intent intent = new Intent(this, MyService.class); 02: startService(intent);

Broadcasting Messages: Intents can send messages to many parts of the app. This is useful for things like system events, such as low battery warnings.

01: Intent intent = new Intent("com.example.CUSTOM_INTENT"); 02: sendBroadcast(intent);

Intent Filters

To receive an implicit intent, an app part must have an intent filter in its settings file. An intent filter tells which types of intents the part can handle.

01: <activity android:name=".MyActivity"> 02: <intent-filter> 03: <action android:name="android.intent.action.VIEW"/> 04: <category android:name="android.intent.category.DEFAULT"/> 05: <data android:scheme="http"/> 06: </intent-filter> 07: </activity>

Intent Extras: Adding Extra Data

Sometimes, you need to pass extra data to the target part. This is done using intent extras.

01: Intent intent = new Intent(this, NewActivity.class); 02: intent.putExtra("key", "value"); 03: startActivity(intent);

Getting Data from Intents

In the target part, you can get the data passed through the intent.

01: Intent intent = getIntent(); 02: String value = intent.getStringExtra("key");

Conclusion

Intents allow different parts of an app to talk to each other acting like some sort of messenger between screeens/Activities [→].

Here is another article you might like 😊 What Are Fragments In Android Development?