> ## Documentation Index
> Fetch the complete documentation index at: https://help.airbridge.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Handling Cases Where Tracking Links Open in WebView - Android SDK (Deprecated)

<Danger>
  This webpage is exclusively for selected customers and should not be shared with others.
</Danger>

## Overview

***

When an Airbridge tracking link opens within your app's WebView environment, the deep linking user experience can be degraded compared to when it opens in another app. This document describes how to resolve this issue by having the Airbridge SDK open the tracking link instead of the WebView when a tracking link is clicked within the WebView environment.

This document will guide you through the following steps to resolve the issue:

1. An explanation of the Airbridge SDK function that opens tracking links.
2. Detecting when a tracking link is clicked within the app's WebView.
3. Configuring the Airbridge SDK to open the tracking link instead.

<Danger>
  **Attention**

  If another company's tracking link is opened in your app, and that other company's app is not installed and the tracking link's fallback is set to a web URL, the fallback web page will open in the user's default browser, not the WebView.
</Danger>

## Airbridge SDK Function for Opening Tracking Links

The Airbridge SDK provides a function called `Placement Click` which allows you to open tracking links from the native part of your application.

You can use this feature with the following code. You can also pass an `AirbridgeCallback<Boolean>` as a parameter to `Airbridge.click` to receive a callback for the response.

<CodeGroup>
  ```kotlin Kotlin lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  var trackingLink: String = "..."
  Airbridge.click(trackingLink)
  ```

  ```java Java lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  String trackingLink = "...";
  Airbridge.click(trackingLink);
  ```
</CodeGroup>

## Detecting When a Tracking Link is Clicked in the WebView

In `WebViewClient`, you can implement `WebViewClient#shouldOverrideUrlLoading` to detect when a specific URL is being loaded and decide whether to proceed with loading it or to stop it. Follow the steps below to implement the code to detect if the URL is an Airbridge tracking link.

📘[WebViewClient Android Developer Documentation](https://www.google.com/search?q=https://developer.android.com/reference/android/webkit/WebViewClient%23shouldOverrideUrlLoading\(android.webkit.WebView,%2520java.lang.String\))

### Using WebViewClient

To implement `WebViewClient#shouldOverrideUrlLoading`, create a `webViewClient` instance and set it to the `webView` using `webView#setWebViewClient`. The result of `WebViewClient#shouldOverrideUrlLoading` returns whether the `WebResourceRequest` was handled. Implement `WebViewClient#shouldOverrideUrlLoading` to detect which URL is being loaded. The URL being loaded is passed in the form of `request.url` from the `WebResourceRequest` function argument.

<CodeGroup>
  ```kotlin Kotlin lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  private val webViewClient = object : WebViewClient() {

      override fun shouldOverrideUrlLoading(
          view: WebView?,
          request: WebResourceRequest?
      ): Boolean {
          return super.shouldOverrideUrlLoading(view, request)
      }
  }

  private fun initWebview() {
      webView.webViewClient = webViewClient
  }
  ```

  ```java Java lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  private final WebViewClient webViewClient = new WebViewClient() {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          return super.shouldOverrideUrlLoading(view, request);
      }
  }

  private void initWebView() {
      webView.setWebViewClient(webViewClient);
  }
  ```
</CodeGroup>

### Identifying an Airbridge Tracking Link

Among the URLs being loaded, identify the ones that are Airbridge tracking links. An Airbridge tracking link will have a URL host of `abr.ge`, `YOUR_APP_NAME.deeplink.page`, or a custom domain.

📘 You can find `YOUR_APP_NAME` in your dashboard under **`Settings > Tokens > App Name`**.

<CodeGroup>
  ```kotlin Kotlin lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  override fun shouldOverrideUrlLoading(
      view: WebView?,
      request: WebResourceRequest?
  ): Boolean {

      request?.url?.let {

          val airbridgeHosts = arrayOf(
              "abr.ge",
              "YOUR_APP_NAME.abr.ge",
              "YOUR_APP_NAME.deeplink.page"
          )

          if (airbridgeHosts.contains(it.host)) {
              return true
          }
      }

      return false
  }
  ```

  ```java Java lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  private final WebViewClient webViewClient = new WebViewClient() {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          Uri uri = request == null ? null : request.getUrl();
          if (uri != null) {
              String host = uri.getHost();

              List<String> airbridgeHosts = Arrays.asList(
                      "abr.ge",
                      "YOUR_APP_NAME.abr.ge",
                      "YOUR_APP_NAME.deeplink.page"
              );

              if (airbridgeHosts.contains(host)) {
                  return true;
              }
          }

          return false;
      }
  };
  ```
</CodeGroup>

## Setting the Airbridge SDK to Open the Tracking Link Instead

***

With the steps above, you can now detect when an Airbridge tracking link is loaded in a WebView. When a link is detected, use the `Airbridge.click` function and return `true` from `webView#shouldOverrideUrlLoading`. This will have the **Airbridge SDK open the tracking link instead of the WebView**.

<CodeGroup>
  ```kotlin Kotlin lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  override fun shouldOverrideUrlLoading(
      view: WebView?,
      request: WebResourceRequest?
  ): Boolean {

      request?.url?.let {

          val airbridgeHosts = arrayOf(
              "abr.ge",
              "YOUR_APP_NAME.abr.ge",
              "YOUR_APP_NAME.deeplink.page"
          )

          if (airbridgeHosts.contains(it.host)) {
              decisionHandler(it)
              return true
          }
      }

      return false
  }

  private fun decisionHandler(uri : Uri) {
      Airbridge.click(uri.toString())
  }
  ```

  ```java Java lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  private final WebViewClient webViewClient = new WebViewClient() {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          Uri uri = request == null ? null : request.getUrl();
          if (uri != null) {
              String host = uri.getHost();

              List<String> airbridgeHosts = Arrays.asList(
                      "abr.ge",
                      "YOUR_APP_NAME.abr.ge",
                      "YOUR_APP_NAME.deeplink.page"
              );

              if (airbridgeHosts.contains(host)) {
                  decisionHandler(uri)
                  return true;
              }
          }

          return false;
      }
  };

  private void decisionHandler(Uri uri) {
      Airbridge.click(uri.toString());
  }
  ```
</CodeGroup>

## Conclusion

***

The implementation is now complete. Below is the full code used in this guide.

<CodeGroup>
  ```kotlin Kotlin lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout....)
      initWebView()
  }

  private fun initWebView() {
      webView.settings.javaScriptEnabled = true
      webView.settings.domStorageEnabled = true
      webView.webViewClient = webViewClient
  }

  private val webViewClient = object : WebViewClient() {

      @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
      override fun shouldOverrideUrlLoading(
          view: WebView?,
          request: WebResourceRequest?
      ): Boolean {

          request?.url?.let {
              val airbridgeHosts = arrayOf(
                  "abr.ge",
                  "YOUR_APP_NAME.abr.ge",
                  "YOUR_APP_NAME.deeplink.page"
              )

              if (airbridgeHosts.contains(it.host)) {
                  decisionHandler(it)
                  return true
              }
          }

          return false
      }

      private fun decisionHandler(uri: Uri) {
          Airbridge.click(uri.toString())
      }
  }
  ```

  ```java Java lines theme={"theme":{"light":"github-dark-dimmed","dark":"github-dark-dimmed"}}
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout....);

      webView = findViewById(R.id....);
      initWebView();
  }

  private void initWebView() {
      webView.getSettings().setJavaScriptEnabled(true);
      webView.getSettings().setDomStorageEnabled(true);
      webView.setWebViewClient(webViewClient);
  }

  private final WebViewClient webViewClient = new WebViewClient() {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
          Uri uri = request == null ? null : request.getUrl();
          if (uri != null) {
              String host = uri.getHost();

              List<String> airbridgeHosts = Arrays.asList(
                      "abr.ge",
                      "YOUR_APP_NAME.abr.ge",
                      "YOUR_APP_NAME.deeplink.page"
              );

              if (airbridgeHosts.contains(host)) {
                  decisionHandler(uri)
                  return true;
              }
          }

          return false;
      }
  };

  private void decisionHandler(Uri uri) {
      Airbridge.click(uri.toString());
  }
  ```
</CodeGroup>

<link rel="alternate" hrefLang="en" href="https://help.airbridge.io/en/developers/open-link-in-webview-deprecated-android-sdk-v2" />

<link rel="alternate" hrefLang="ko" href="https://help.airbridge.io/ko/developers/open-link-in-webview-deprecated-android-sdk-v2" />
