Implementing Deep Linking in XML-Based Apps

Deep linking is a technique that enables users to navigate directly to a specific location within a mobile application from an external source, such as a web page, email, or another app. This enhances user experience by bypassing the need for users to manually navigate through the app. In XML-based Android apps, deep linking is implemented using intent filters in the AndroidManifest.xml file.

What is Deep Linking?

Deep linking creates a direct pathway to a specific section or content within a mobile application. Instead of simply opening the app, a deep link can open a specific screen, pre-fill a form, or navigate to a particular product page. This can significantly improve user engagement and retention.

Why Use Deep Linking?

  • Improved User Experience: Directs users straight to the relevant content, reducing friction.
  • Increased Engagement: Facilitates seamless navigation from external sources to specific app sections.
  • Better Marketing Campaigns: Allows for targeted promotions and customized experiences.
  • Simplified Sharing: Makes it easier to share specific content within an app.

How to Implement Deep Linking in XML-Based Apps

To implement deep linking in an XML-based Android application, follow these detailed steps:

Step 1: Update AndroidManifest.xml

Declare intent filters within the activity you want to deep link to. These intent filters specify the scheme, host, and path that the activity will handle.


<activity
    android:name=".DetailActivity"
    android:exported="true"> <!-- Ensure the activity is exported -->
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:scheme="https"
            android:host="www.example.com"
            android:pathPrefix="/details" />
    </intent-filter>
</activity>

Explanation of the attributes:

  • <action android:name="android.intent.action.VIEW" />: Specifies that this activity can display data.
  • <category android:name="android.intent.category.DEFAULT" />: Ensures the activity is included in implicit intents.
  • <category android:name="android.intent.category.BROWSABLE" />: Allows the activity to be launched from a browser.
  • <data android:scheme="https" android:host="www.example.com" android:pathPrefix="/details" />: Defines the URL scheme, host, and path prefix that the activity will handle.
  • android:exported="true": It’s imperative to set exported to true for your Activity to receive intents from other apps. Omission can prevent your deep link from functioning correctly.

Step 2: Handle the Intent in the Activity

In the corresponding activity (e.g., DetailActivity), retrieve the data from the intent and process it to display the relevant content.


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;

public class DetailActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        TextView detailTextView = findViewById(R.id.detailTextView);

        Intent intent = getIntent();
        if (intent != null && intent.getData() != null) {
            Uri data = intent.getData();
            String itemId = data.getLastPathSegment(); // Assuming the ID is the last segment
            // Load item details using the itemId
            String detailText = "Details for item ID: " + itemId;
            detailTextView.setText(detailText);
        } else {
            detailTextView.setText("No item ID received.");
        }
    }
}

In this code:

  • The activity retrieves the intent using getIntent().
  • It checks if the intent contains data using intent.getData().
  • The data (URI) is retrieved, and the item ID is extracted using data.getLastPathSegment().
  • The item details are loaded based on the extracted ID and displayed in the UI.

Step 3: Generate Deep Links

Create deep links that match the defined scheme, host, and path in the intent filter. These links can be used in web pages, emails, or other apps.

Example Deep Link:

https://www.example.com/details/12345

In this example, 12345 is the item ID. When a user clicks this link, it will open the DetailActivity and pass 12345 as the item ID.

Step 4: Testing Deep Links

Test the deep links to ensure they navigate correctly to the intended activity and display the appropriate content. You can test using the Android Debug Bridge (ADB) or through an HTML page with a link.

Using ADB:


adb shell am start -W -a android.intent.action.VIEW -d "https://www.example.com/details/12345" com.example.deeplinking

Explanation:

  • adb shell am start: Executes an activity manager command in the shell.
  • -W: Waits for the launch to complete.
  • -a android.intent.action.VIEW: Specifies the action to view data.
  • -d "https://www.example.com/details/12345": Defines the data URI.
  • com.example.deeplinking: Specifies the package name of your app.

Handling Multiple Schemes and Hosts

To support multiple schemes and hosts, define multiple <data> elements within the same <intent-filter>.


<activity
    android:name=".DetailActivity"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:scheme="https"
            android:host="www.example.com"
            android:pathPrefix="/details" />
        <data
            android:scheme="myapp"
            android:host="open"
            android:pathPrefix="/item" />
    </intent-filter>
</activity>

This configuration supports both https://www.example.com/details/12345 and myapp://open/item/12345.

Securing Deep Links

To prevent other apps from intercepting your deep links, you can use the android:autoVerify="true" attribute in your intent filter. This requires you to host a Digital Asset Links file on your website.


<activity
    android:name=".DetailActivity"
    android:exported="true">
    <intent-filter android:autoVerify="true">
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data
            android:scheme="https"
            android:host="www.example.com"
            android:pathPrefix="/details" />
    </intent-filter>
</activity>

Digital Asset Links

To implement asset link verification, you need to create a assetlinks.json file and host it at https://www.example.com/.well-known/assetlinks.json.


[
  {
    "relation": ["delegate_permission/common.handle_all_urls"],
    "target": {
      "namespace": "android_app",
      "package_name": "com.example.deeplinking",
      "sha256_cert_fingerprints":
        ["YOUR_SHA256_CERTIFICATE_FINGERPRINT"]
    }
  }
]

Replace YOUR_SHA256_CERTIFICATE_FINGERPRINT with your app’s SHA256 certificate fingerprint. You can obtain this fingerprint using the keytool command.


keytool -list -v -keystore your_keystore.jks -alias your_alias

Common Pitfalls

  • Incorrect Scheme or Host: Ensure the scheme and host in the intent filter match the deep link URL.
  • Missing Browsable Category: Without the BROWSABLE category, the activity cannot be launched from a browser.
  • Data Handling: Handle the intent data carefully, especially when extracting parameters from the URI.
  • Exported Attribute: Always check `android:exported=true` for each activity with Intent-Filter.
  • Asset Link Verification Issues: Ensure the assetlinks.json file is correctly hosted and the SHA256 fingerprint is accurate.

Conclusion

Implementing deep linking in XML-based Android apps involves updating the AndroidManifest.xml with intent filters, handling the intent in the corresponding activity, and creating deep links that match the defined patterns. Proper testing and security measures, such as asset link verification, are crucial to ensure a seamless and secure user experience. By following these guidelines, you can enhance your app’s usability, engagement, and marketing effectiveness.