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

Deep Linking in Android with Example

Deep linking in Android allows an app to be started up and navigated to a specific activity using a URL. This is useful for linking users directly from a website, an email, or another app to a particular location within your app.

There are three types of deep linking methods available in Android:

  1. Traditional Deep Linking: Uses a standard URL to launch the app.
  2. Android App Links: A more advanced form of deep linking that allows your app's website URLs to open your app by default.
  3. Firebase Dynamic Links: Uses Firebase to create links that can survive the installation process and take a user to a specific location within the app after it��s installed.

Here's a simple example of how to implement traditional deep linking:

1. Update the Manifest:

Modify your AndroidManifest.xml to include an intent filter inside the <activity> tag that you want to be deep-linked.

<activity android:name=".DeepLinkedActivity">
    <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:host="example.com"
            android:scheme="https"
            android:pathPrefix="/path/to/content" />
    </intent-filter>
</activity>

This configuration means the app will listen for links that are structured like: https://example.com/path/to/content.

2. Handle the Intent in Your Activity:

In the targeted Activity, you need to handle the incoming intent and extract data:

class DeepLinkedActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_deep_linked)

        intent?.let {
            handleDeepLink(it)
        }
    }

    private fun handleDeepLink(intent: Intent) {
        val action = intent.action
        val data = intent.dataString

        if (Intent.ACTION_VIEW == action && data != null) {
            // Handle the deep link. You might extract parameters from the URL and use them in your app.
        }
    }
}

Testing:

To test deep linking, you can use adb (Android Debug Bridge) by running the following command in your terminal:

adb shell am start -W -a android.intent.action.VIEW -d "https://example.com/path/to/content" your.package.name

Replace your.package.name with your app's package name.

Notes:

  • Deep links can potentially be a security concern. Always validate data passed in from deep links.

  • Android App Links are a more advanced version that requires website verification, ensuring only the app associated with the website can handle its deep links without user prompts.

  • Firebase Dynamic Links are useful when you want the link to work even if the user needs to install the app first. The link will survive the app installation process.

  1. Setting up deep linking in Android manifest:

    • Description: Declare deep link intents in the AndroidManifest.xml file to specify the activities that can be launched via deep links.
    • Code (AndroidManifest.xml):
      <activity
          android:name=".DeepLinkActivity"
          android:label="Deep Link Activity">
          <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="http" android:host="example.com" android:pathPrefix="/deep-link" />
          </intent-filter>
      </activity>
      
  2. Handling deep links in Android activities:

    • Description: In the target activity, extract and process the data from the deep link URI.
    • Code (Kotlin - DeepLinkActivity):
      class DeepLinkActivity : AppCompatActivity() {
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_deep_link)
      
              // Extract data from the deep link
              val uri = intent.data
              val path = uri?.path
              val host = uri?.host
              // Process the deep link data
          }
      }
      
  3. Customizing deep link behavior in Android:

    • Description: Customize the behavior of your app when handling deep links, such as parsing additional parameters or performing specific actions.
    • Code (Kotlin - DeepLinkActivity):
      class DeepLinkActivity : AppCompatActivity() {
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_deep_link)
      
              // Extract and customize deep link behavior
              val uri = intent.data
              val path = uri?.path
              val host = uri?.host
              // Customize behavior based on the deep link data
          }
      }
      
  4. Passing parameters through deep links in Android:

    • Description: Include parameters in the deep link URI to pass additional data to the target activity.
    • Code (AndroidManifest.xml):
      <data android:scheme="http" android:host="example.com" android:pathPrefix="/deep-link/{param}" />
      
      Code (Kotlin - DeepLinkActivity):
      class DeepLinkActivity : AppCompatActivity() {
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_deep_link)
      
              // Extract parameters from the deep link
              val uri = intent.data
              val paramValue = uri?.lastPathSegment
              // Process the parameter value
          }
      }
      
  5. Using URI schemes for deep linking in Android:

    • Description: In addition to HTTP/HTTPS deep links, you can use custom URI schemes for deep linking.
    • Code (AndroidManifest.xml):
      <data android:scheme="myscheme" android:host="myhost" android:path="/deep-link" />
      
      Code (Kotlin - DeepLinkActivity):
      class DeepLinkActivity : AppCompatActivity() {
          override fun onCreate(savedInstanceState: Bundle?) {
              super.onCreate(savedInstanceState)
              setContentView(R.layout.activity_deep_link)
      
              // Extract data from the deep link with custom URI scheme
              val uri = intent.data
              val path = uri?.path
              val host = uri?.host
              // Process the deep link data
          }
      }
      
  6. Deferred deep linking in Android example:

    • Description: Implement deferred deep linking to handle scenarios where the app is not installed when the deep link is first clicked.
    • Code (Kotlin - Application class):
      class MyApplication : Application() {
          override fun onCreate() {
              super.onCreate()
              AppLinkData.fetchDeferredAppLinkData(this) { appLinkData ->
                  if (appLinkData != null) {
                      // Handle deferred deep link data
                      val uri = appLinkData.targetUri
                      // Process the deferred deep link data
                  }
              }
          }
      }