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.
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.
in MainActivity i use this code
if(getIntent().getDataString()!=null &&
getIntent().getDataString().contains("specialWordInDeepLink")){
// confirm is run by deeplink
}
Inside any activity you have specified the intent filter, you can get the URL via the Intent that launched the activity.
Activity:-
Intent appLinkIntent = getIntent();
Uri appLinkData = appLinkIntent.getData();
Fragment:-
Intent appLinkIntent = requireActivity().getIntent();
Uri appLinkData = appLinkIntent.getData();
When a clicked link or programmatic request invokes a web URI intent, the Android system tries each of the following actions, in sequential order, until the request succeeds:
Open the user's preferred app that can handle the URI, if one is designated. Open the only available app that can handle the URI. Allow the user to select an app from a dialog.
To create a link to your app content, add an intent filter that contains these elements and attribute values in your manifest:
https://developer.android.com/training/app-links/deep-linking
Check out official android doc to know more about deeplinking
© 2022 - 2024 — McMap. All rights reserved.