I have added a deep link to my app which maps an activity to a particular web page on my website (Link referred: https://developer.android.com/training/app-links/deep-linking ).
While handling the deep link in my Java code, getAction()
and getData()
methods give me null value.
I tried testing it here: https://firebase.google.com/docs/app-indexing/android/test (This gave me perfect result) But the same link opens in A web browser rather than in my app when clicked.
Android Manifest code :
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:exported="true">
<tools:validation testUrl="https://www.mywebsite.com/xyz" />
<intent-filter android:autoVerify="true">
<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.mywebsite.com"
android:pathPrefix="/xyz" />
</intent-filter>
</activity>
Java Code :
Intent intent = getIntent();
String action = intent.getAction(); // getting null value here
Uri data = intent.getData(); // getting null value here
I want that if the app is present, then it should be opened when the link is clicked or else the link will open the web page. Where and what am I missing?
therefore intent is always null in constructor. After setIntent(null): It's possible to change intent from outside of activity with setIntent() . In all other cases it can't.
Deeplink Mobile allows mobile app users to generate a shareable link to a page in your app. The link is handled by a redirect page (from component Deeplink Web). On mobile, the redirect page opens your mobile app on a specific page using deeplinks or opens the App store / Play store to download your mobile app.
In the context of mobile apps, deep linking consists of using a uniform resource identifier (URI) that links to a specific location within a mobile app rather than simply launching the app. Deferred deep linking allows users to deep link to content even if the app is not already installed.
Deep linking is the extension or usage of attribution data. Let's go through a basic example of how it works to illuminate the tech. In the explanation previously, we talked about how a user may click a link or an ad: The attribution vendor then points the user to the appropriate app store to download the app.
You can run into this problem because using singleTask
. In that case you should use onNewIntent to 'update' intent, because onCreate
and onResume
will not handle it.
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the Intent#FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
//TODO: do something with new intent
}
instead of getting intent data in onCreate, get in onResume
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
if (intent != null && intent.getData() != null ){
Toast.makeText(this, intent.getData().toString(), Toast.LENGTH_SHORT).show();
webView.loadUrl(intent.getData().toString());
}
}
add these lines of code which activity is attached for deep linking to open your website page
and mark it as a answer if it helped you to solve your problem
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