So 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.
Android offers a deep linking mechanism for that purpose. This mechanism is provided by the basic Deep Links and more efficient Android App Links.
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:
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:
<?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.
Now it’s time for the next step – to create a “main” app:
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:
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:
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.
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.
Martin Smith says:
Good to know about this info, thanks for sharing the great article…