Search code examples
androidperformanceandroid-layoutandroid-fragmentsdeep-linking

How to give Intent Data in Deep Link


I have updated manifest file to support deep linking. I have checked it from Run(Edit Configuration), and it opens the specified activity.

Now i need to send some data with the deep link. So what should be the procedure of it. I have added another data attribute, but i don't understand how to get data in the Activity in the same key/value fashion.

I am getting Intent like this in Activity

   Intent intent = getIntent();
    String action = intent.getAction();
    Uri data = intent.getData();

intent.getData() has this value= myapp://videodeeplink

I have read some articles and tutorials regarding this, but i am just unable to get this. Kindly guide me how to put and get some data in deep linking.

myapp://videodeeplink

<activity
    android:name=".VideosListActivity"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme" >

    <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="myapp" android:host="videodeeplink"/>
        <data android:scheme="videoURL" android:host="videoURL"/>
    </intent-filter>


</activity>

Solution

  • To send the data you should add pathPrefix parameter in your data tag like below, so that later we can parse it in the Activity/Fragment where you call it.

     <data
         android:host="@string/host"
         android:pathPrefix="path"
         android:scheme="@string/schema" />
    

    Now when you want to parse it you can use pathSegments in Android, like below using the intents to obtain the data inside.

    mainIntent = getIntent();
    if (mainIntent!=null && mainIntent.getData()!=null
        && (mainIntent.getData().getScheme().equals("http"))){
            Uri data = mainIntent.getData();
            List<String> pathSegments = data.getPathSegments();
            if(pathSegments.size()>0)
            String prefix=pathSegments.get(0); // This will give you prefix as path
    }