I have an application where I need to do different task on different launch types. How to detect that app has been launched by deeplink, when my app was in background.
When a user click an URL, it might open a dialog which asks the user to select one of multiple apps handling the given URL. On the other hand, An Android App Link is a deep link based on your website URL that has been verified to belong to your website. When user clicks that URL, it opens your app.
Unlike internet URLs and web addresses that are standardized across devices and platforms, deep links aren't universal. To launch your Android app, you'll need a different deep link from one needed to launch your iOS app.
To access the Deep Link Validator:In Google Ads, click Tools. Find “App Advertising Hub” under “Planning". You'll find the Deep Link Validator on the menu.
Assuming you already have an activity ready with the required intents what you'll to do is check your activity's if(getIntent().getAction() != null)
which means it was launched via a deep-link. Normal intents used for navigation will return null
.
Now the issue is if your activity was already running in background and you wrote this code in onCreate()
, and the deep-linked activity was set to android:launchMode="singleTask"
or launched with a FLAG_ACTIVITY_SINGLE_TOP
it won't trigger again.
For this you will have to override onNewIntent(Intent intent)
method of your activity, this way you can know each time your activity is started from an intent.
Again, here you can check if(intent.getAction() != null)
and intent.getData()
to retrieve the data.
One thing to note is to avoid running the same code twice in onCreate
and onNewIntent
In case you haven't implemented deep-linking in your app yet you will first need to make use of <intent-filter>
to make an activity be available to handle the intent when clicking on a link etc. as an example
<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:host="www.example.com"
android:pathPattern="/.*"
android:scheme="https" />
</intent-filter>
You can read more about on the official Docs here as someone already suggested.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With