App Links Assistant Android Studio open in first app activity

1.5k views Asked by At

I use the 'App Links Assistant' in Android Studio to configure links that will be opened through my app.

Everything works well.

But my problem is that when I open the link through the app, it opens the activity defined within the previous app where I clicked on the link.

For example:

If I click on a link in WhatsApp I see something like this:

open link

And when I open using the app, the app opens within WhatsApp as seen in this image:

whatsapp opens

So how can I open the app only in My app and not within WhatsApp or another app?

Here my AndroidManifest.xml code of this activity:

 <activity android:name=".extraLinks"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        >
        <intent-filter>
            <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"
                android:host="www.jtube.live" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

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

            <data
                android:scheme="https"
                android:host="www.jtube.live" />
        </intent-filter>
       
    </activity>

update: I checked my Gmail and Chrome apps and if I open from those apps its working well, so my problem with Whatsapp app only...

2

There are 2 answers

0
Николай Гольцев On BEST ANSWER

When activity A starts activity B the default behavior is:

A new instance of activity B is created within a current task and pushed to the back stack.

To customize such behavior there are some options:

  • add flags to the intent that starts a specific activity;
  • set launchMode attribute for an activity being started;

The first option is out of your control when we talk about other applications.

You face different behavior within WhatsApp and Gmail applications because of the following:

Gmail customizes intents for every HTTP link with FLAG_ACTIVITY_NEW_TASK and consequently, activity in your application starts within a new task. WhatsApp application doesn't specify this flag when it starts any activity which handles a specific HTTP link.

In your case, the only way to customize this behavior is to play with launchMode attribute for target activity that handles the app link. There are two possible values applicable to your case: singleTask or singleInstance.

Check these links to understand how it works:

1
Joseph Utulu On

try this:

<intent-filter android:label="app_label">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.LAUNCHER"/>
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />

    <data
       android:scheme="https"
       android:host="www.jtube.live" />
</intent-filter>