Deep Links in Android

deeplinkandroid
Every business has to look for new ways of user’s acquisition nowadays. It is the time when everybody understands that the more engaging interaction you offer the more likely users will spend their money on your product. According to eMarketer, users prefer to spend time within a mobile app instead of mobile browsers.image2So the question: what is more engaging – web page or an app – becomes very clear. As different sources report, we can realize that any solutions that allow users to address issues inside an app instead of a web page have big potential for business growth and make people spend more money within apps.image1Android offers a deep linking mechanism for that purpose. This mechanism is provided by the basic Deep Links and more efficient Android App Links.

How Do Deep Links work

Deep Links are URLs that take users directly to specific content in an app. The main disadvantage of this approach that if there are other apps that “understand” your URL, a user may choose them to open the link.Android App Links allow an app to define itself as a default handler of the given type of link. But this approach is allowed only from Android 6.0 (API level 23).

Both approaches based on the URI scheme definition. Look at the following URL:

https://www.vironit.com/demo?userid=17&client=android

This URL can be counted as a deep link. It contains the following parts:

To allow your app to open specified URLs, you need to provide the following steps:
  • Add <intent-filter> to one of your activities in Android Manifest;
  • In <action> section add ACTION_VIEW intent action;
  • For <category> section include BROWSABLE and DEFAULT categories.;
  • In <data> section enter your URL parts described above: scheme, host (and pathPrefix if you need one);
  • Finally, add the launchMode attribute to the singleTask activity to avoid multiple instances of the same activity.
Let’s create a simulation for the most popular use case of deep link – opening an app via URL on the screen with particular goods:
  • Create an Android project;
  • Name it as you like because this app will provide us to the “main” app and its name doesn’t matter;
  • In XML add EditText and Button.
<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout

   xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:app="http://schemas.android.com/apk/res-auto"

   xmlns:tools="http://schemas.android.com/tools"

   android:layout_width="match_parent"

   android:layout_height="match_parent"

   tools:context=".MainActivity">

 

   <EditText

       android:id="@+id/et_message"

       android:layout_width="0dp"

       android:layout_height="wrap_content"

       android:layout_marginStart="8dp"

       android:layout_marginTop="8dp"

       android:layout_marginEnd="8dp"

       android:ems="10"

       android:hint="Name"

       android:inputType="textPersonName"

       app:layout_constraintEnd_toEndOf="parent"

       app:layout_constraintStart_toStartOf="parent"

       app:layout_constraintTop_toTopOf="parent"/>

 

   <Button

       android:id="@+id/btn_share"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_marginStart="8dp"

       android:layout_marginTop="8dp"

       android:layout_marginEnd="8dp"

       android:layout_marginBottom="8dp"

       android:text="Share"

       app:layout_constraintBottom_toBottomOf="parent"

       app:layout_constraintEnd_toEndOf="parent"

       app:layout_constraintStart_toStartOf="parent"

       app:layout_constraintTop_toBottomOf="@+id/et_message"/>

 

</android.support.constraint.ConstraintLayout>
EditText will be used for entering data to provide it in “main” app. You may think about it as the case when a user interacts with an app and decides to share it with his friends. To make this example more real you can name the Button “share”. In onCreate() method add the following code:
override fun onCreate(savedInstanceState: Bundle?) {

   super.onCreate(savedInstanceState)

   setContentView(R.layout.activity_main)

 

   val et = findViewById<EditText>(R.id.et_message)

 

   findViewById<Button>(R.id.btn_share).setOnClickListener {

       Intent(Intent.ACTION_VIEW)

           .apply {

               data = Uri.parse("https://vironit.com/" + et.text.toString())

               if (this.resolveActivity(packageManager) == null) {

                   Toast.makeText(this@MainActivity, "No activity to open intent", Toast.LENGTH_SHORT).show()

               } else {

                   startActivity(this)

               }

           }

   }

}
Resolving existing activity in a conditional branch is the best practice to avoid crash on devices where there are no activities available to open an URL.
  • Start the emulator and run the project;
  • Enter something into the EditText and click the “Share” button;
  • You will see app-selection dialog, where you can choose an app to open the created URL.
Now it’s time for the next step – to create a “main” app:
  • Start a new project.
  • Name it “MainApp” because it is a simulation of our app that we have in a GooglePlay.
Let’s imagine that the MainActivity provides a list of different goods. So, to show a details screen about particular goods we need another activity:
  • Create a new activity called DetailsActivity. In its XML add a TextView;
  • Now go to Manifest file and declare this activity;
Add following intent-filter to activity:
<activity android:name=".DetailsActivity"

         android:launchMode="singleTask">

   <intent-filter>

       <action android:name="android.intent.action.VIEW"/>

       <category android:name="android.intent.category.BROWSABLE"/>

       <category android:name="android.intent.category.DEFAULT"/>

       <data android:scheme="https" android:host="vironit.com"/>

   </intent-filter>

</activity>
This intent-filter allows the DetailsActivity to handle URLs. Now DetailsActivity can be opened by clicking on URL.The last step is to extract any data from the link directly in an app. Add following code in onCreate() method of DetailsActivity:
override fun onCreate(savedInstanceState: Bundle?) {

   super.onCreate(savedInstanceState)

   setContentView(R.layout.activity_details)

 

   val tv = findViewById<TextView>(R.id.text)

 

   val data = intent.data

   data?.let {

       tv.text = it.lastPathSegment

   }

 

}
We don’t provide any navigation from MainActivity into DetailsActivity to emphasize deep linking mechanism: an activity that is not the launcher in the app can be an entry point into an app.

Now run the “MainApp” on the same emulator (you can use a real device as well) with the previously installed app. You will see just a screen of MainActivity.

Open the first app, type something in the EditText field and click the “Share” button:

You see our app as a choice for opening our URL. Hope you have already realized that we can provide the id of any product and load its details instead of any data. That is how deep links work in Android.

To use Android App Links (for Android 6.0 and higher) you have to:
  • Add the autoVerify=true attribute to the intent-filter of the activity that handles deep links
  • Add a Digital Asset Links JSON file to your website
The second step links your intent-filters with your website pages.You can use one interesting trick to be sure that only your app will be able to open particular URI. Just change the scheme from “https” to something like “myapp” in <data> field for DetailsActivity and in the intent of the first app.In the first app:
data = Uri.parse("myapp://vironit.com/" + et.text.toString())
In the MainApp:
<data android:scheme="myapp" android:host="vironit.com"/>
Install both apps and run the first one. The system will not offer you to choose an app for our custom URI because it “knows” that there is only your MainApp can open URI with “myapp” scheme. So you will be redirected into your MainApp without any intermediate dialogues.

Best Practices for Deep Linking

  • It would be very optimistic to rely on Android App Links only, since there are a lot of devices with Android versions earlier than 6.0 on the market, so the basic Deep Linking mechanism is a good choice in any case.
  • You need to remember that some messengers don’t allow to open external apps with a deep link that has “http” scheme;
  • Always validate the deep link because it may navigate to secure part of your app, so check link parameters before dealing with it.
  • Try to make a deep link as a separate entry point into your app. It makes your app more testable and separated logically.
When users interact with their smartphones they expect intuitive behavior. Deep Links provide satisfying user experience and make people believe that all the components on the phone are connected with each other. This fact increases the overall users’ engagement and allows businesses to make life better in different ways.
Please, estimate my article. I did my best!
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 3.33 out of 5)
Loading…
  • Martin Smith

    Good to know about this info, thanks for sharing the great article…

Share
Share