Implementing Deep Linking In Your Android Aplication
Posted By : Akash Tomer | 28-Mar-2018
There is a situation where you want to open an app when you click on the link. Suppose you want to verify an email or reset a password after verifying a user you need to redirect to your app. For that, deep linking is required to open an app when a user clicked on the link.
First, we have to add an intent filter to that activity which is deep-linked to a link. In an intent filter, you have to add action.VIEW, category.DEFAULT and category.BROWSEABLE . Below is the following code which you have to add in manifest file of your app.
<activity
android:name=".view.activity.LogInActivity"
android:screenOrientation="portrait">
<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:host="host_name" android:scheme="http"android:prefix="prefix"
/>
</intent-filter>
</activity>
In the data tag you have to specify the host as www.example.com and if an app is deep-linked with more than one link having the same hostname then you must you prefix to differentiate the links. Suppose you have to deep link www.example.com/lognin for login and www.example.com/signup for signup. Now you have the same hostname for that which activity is open when you clicked on a link for that we will you the prefix parameter of data tag.
You have to set the launchMode tag to single task otherwise it will open the multiple instances of your app.Now if you want to get the intent filter you have to get the intent in your activity by the following lines of code.
public class LogInActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (intent.getBooleanExtra(DeepLink.IS_DEEP_LINK, false)) {
Bundle parameters = intent.getExtras();
String idString = parameters.getString("id");
// Do something with the ID...
}
...
}
}
you also have to override the onNewIntent method of the activity beacuse only one instance of the app is created so you have to recieve that intent filter in onNewIntent method.
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
}
Hope this will help you
Thanks
About Author
Akash Tomer
Akash is an Android Developer at Oodles Technology.