Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

What is Intent in Android?

In Android, an Intent is a messaging object you can use to request an action from another app component. It acts as a mechanism to abstract an operation to be performed, serving as a bridge between separate components (such as activities, services, or broadcast receivers).

Types of Intents:

  1. Explicit Intents: These specify the component to start by name (i.e., the fully-qualified class name). They are typically used for application-internal operations.

    Intent intent = new Intent(this, TargetActivity.class);
    startActivity(intent);
    
  2. Implicit Intents: These do not name a specific component. Instead, they declare a general action to perform, allowing a component from another app to handle them.

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.example.com"));
    startActivity(intent);
    

In the example above, if a web browser is available on the device, it will be launched to open the given URL.

Common Uses of Intents:

  1. Starting Activities: Using startActivity(), you can launch a new activity.

  2. Starting Services: With startService(), you can start a background service.

  3. Delivering Broadcasts: Broadcasts can be sent with sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().

  4. Data Transfer: Intents can carry data payloads of various types within an Extras bundle. For example, you might send integer data with intent.putExtra("key", value).

  5. Task Management: Intents can be used with the task and back stack manager to create new task stacks or bring existing tasks to the foreground.

  6. Common Actions: There are predefined actions in the Intent class that allow for common operations, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, and others.

  7. Integration with Other Apps: For instance, if you want to show a location on a map, you can use an intent to request that another capable app show that location.

Filters and Matching:

For implicit intents, Android determines the appropriate component (e.g., which app or activity should handle the intent) by comparing the intent to intent filters defined in the manifest file of other apps on the device.

An IntentFilter specifies the types of intents that a component can handle. It describes a combination of the following:

  1. Actions: The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, etc.
  2. Categories: Provides an additional way to characterize the activity handling the intent, beyond the primary action itself.
  3. Data: Specifies the URI and the data type (MIME type) of the intent.

If the intent matches the criteria defined in the IntentFilter, that component becomes a candidate to respond to the intent.

In conclusion, Intent in Android is a fundamental concept that provides a powerful way to perform late runtime binding between components, facilitating flexible application architectures and integration points between apps.

  1. Using Intent for Activity Navigation in Android:

    • Use Intent to navigate between activities.
    • Example:
      Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
      startActivity(intent);
      
  2. Passing Data Between Activities with Intent in Android:

    • Use putExtra() to attach data to an intent.
    • Example:
      Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
      intent.putExtra("key", "value");
      startActivity(intent);
      
    • Retrieve data in the target activity using getIntent().getExtras().
  3. Launching External Activities with Intent in Android:

    • Use implicit intents to launch activities outside your app.
    • Example:
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
      startActivity(browserIntent);
      
  4. Intent Flags and Their Uses in Android:

    • Flags modify the behavior of the intent.
    • Example:
      Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
      startActivity(intent);
      
  5. Intent Services and Background Tasks in Android:

    • IntentService is a subclass of Service for handling asynchronous tasks off the main thread.
    • Example:
      Intent intent = new Intent(CurrentActivity.this, MyIntentService.class);
      startService(intent);
      
  6. Customizing Intent Actions and Categories in Android:

    • Define custom actions and categories for your components in the manifest.
    • Example:
      <intent-filter>
          <action android:name="com.example.custom.ACTION"/>
          <category android:name="android.intent.category.DEFAULT"/>
      </intent-filter>