Search code examples
androidintentfilter

How to handle app links on different activities?


I have, say, domain example.com. It can work with both https and http. And I have Android client for this service. I want client to be able to open several link types. Here they are:

http://example.com
https://example.com
http://example.com/app
https://example.com/app

These four should be opened on the ListActivity. But also there are another links like:

https://testask.com/i/__some_guid__
https://testask.com/item/__some_guid__
and relevant link without https

They should be opened on another activity, say, DetailsActivity. Currently I have following intent filter for DetailsActivity:

<intent-filter
    android:autoVerify="true"
    tools:targetApi="m">
    <action android:name="android.intent.action.VIEW" />

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

    <data android:scheme="http" />
    <data android:scheme="https" />
    <data
         android:host="@string/root_host_endpoint"
         android:pathPattern="/i/.*" />
    <data
         android:host="@string/root_host_endpoint"
         android:pathPattern="/item/.*" />
</intent-filter>

And looks like it works correctly. But I don't understand, how to add MainActivity intent filter to point on root host url and not to overlap DetailsActivity intent filter.


Solution

  • Welp, after some experiments I found that following intent filter is working as expected:

            <intent-filter
                android:autoVerify="true"
                tools:targetApi="m">
                <action android:name="android.intent.action.VIEW" />
    
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
    
                <data android:scheme="http" />
                <data android:scheme="https" />
                <data android:host="@string/root_host_endpoint" />
                <data
                    android:host="@string/root_host_endpoint"
                    android:path="/" />
                <data
                    android:host="@string/root_host_endpoint"
                    android:path="/app" />
            </intent-filter>